General Question

klaas4's avatar

How to mimic jQquery's appendTo with a regular expression?

Asked by klaas4 (2194points) July 8th, 2011

Hi!

I use WordPress to manage a website and I’m currently (still… :) ) developing a theme. Now, I wrap some content in a div with a specific class to box it, but I want to append a clear:both-div to that div in case there are any floating elements. I use jQuery as a temporary solution, like so:

$(’.block’).append(’<div class=“clearboth”></div>’);

I figure this can also be done with another way. Problem is, I have no clue how I’d go about something like this. Can I use any WordPress functions or would a regex be appropriate here?

I think it has to go something like this.

• Find all <div> start tags ”<div ”
• Find class=” or class=’
• Look for the class name (e.g. ”block”) before or .
• Then append the clear:both-div just before the corresponding </div> tag.

I hope someone can help.

Thanks!

Davey

Observing members: 0 Composing members: 0

1 Answer

funkdaddy's avatar

So many ways to tackle this, it’s really tough to say what’s best in your situation.

Usually I try to avoid the clearing elements anymore and instead float the container just like the floated content I want it to wrap.

If I needed to add a clearing element, I’d add that to my theme so it’s handled on the backend (with PHP instead of javascript). Is there any reason not to simply add it to all of the containers like that?

Or, how are you choosing which ones to add the “block” class to? Couldn’t you use that same flag to simply add the clearing element as well?

I don’t think a regular expression is the best way to go here, typically that would be used if you didn’t have control of the content from the start and wanted to do the processing afterwards. Even then, I’d use it as a last resort and would rather go with something like the jQuery solution you’ve come up with for consistency.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther