2

The CMS I'm using doesn't seem to apply a class name if the element is floated left or right. I was hoping to not have to mess with the TinyMCE code stuff, and instead do this with jQuery.

Right now, when I align an image to the left or right, it will add the styles inline with the image like so:

<img src="image.jpg" style="float:left">

I was hoping that for those elements that have that style applied, I could add a class name of "alignleft". I tried this, but it does not work:

$( 'a[ style=' + 'float: left;' + ']' ).addClass( 'alignleft' );

Thoughts?

1
  • 1
    I don't know why you're concatenating one string with another instead of just writing the string in the first place 'a[ style=float: left;]' Commented Dec 24, 2013 at 0:16

3 Answers 3

4

I'd suggest selecting the possible elements that might be float: left and then filtering to find those that are, and adding the class-name of alignleft to those:

$('a, img').filter(function(){
    return this.style.float === 'left';
}).addClass('alignleft');

This approach avoids the requirement that the style attribute must begin with the, and be exactly, style="float: left;", including the precise white-space.

Of course, this approach does require that the float: left is assigned in the inline style attribute; if there's a chance that it might be assigned in a stylesheet, whether external or in the head of the page, then you'd have to use jQuery's css() method (which can call getComputedStyle() or currentStyle() to parse those stylesheets (unless you want to do so yourself), leading to:

$('a, img').filter(function(){
    return $(this).css('float') === 'left';
}).addClass('alignleft');

References:

Sign up to request clarification or add additional context in comments.

5 Comments

Great in theory but this.float is undefined.
"This approach avoids the requirement that the style attribute must begin with the, and be exactly, style="float: left;", including the precise white-space." <- what about using ~= instead of = then?
@ant: that would work, certainly, but I'm not sure what the benefit would be? The above approaches avoid parsing the string of the style attribute, which seems/feels better to me. I Can't offer any evidence that it's any faster, or actually better, but it's how I prefer to do it is all.
@antindexer: ~= would still not match if there aren't any whitespaces. you have to use a combined version of the attribute selector i would suggest. See popnoodles answer, I added a second code example.
@MarkusKottländer if yu see my answer I provide link about selectors so it is just food for brain. I hope other part can be distinguished by the person who asks question.
2

Try to put double quotation mark after = sign and and before closing ] symbol. And style~= should be used instead of = sign. This means style attribute contains float:left string.

$( 'a[ style~="' + 'float: left;' + '"]' ).addClass( 'alignleft' );

Here is working example in jsFiddle.

For them who concern:

if you considering the float: left or float:left can not be distinguished by attribute selector. Watch this:

$( 'a[style*="' + 'float:left' + '"], a[style*="' + 'float: left' + '"]' ).addClass( 'alignleft' );

This will work for everything with space or with out the space. See here if your are short of understanding what I want to explain here. Of course some developers with curvehands.dll installed can say that it will not work for more then one space in attribute parameter. Can consider to use what ever they like and give answer as they whanted.

EDIT. Read more about selectors here.

6 Comments

This won't work. The style attribute is float:left which will not be caught in that selector.
But why are you concatenating two strings? I know that's how the OP wrote it, but...it still doesn't make sense, unless I'm missing something?
First of all it is given example by OP. And in future 'float: left;' will be changed string variable I guess.
to @popnoodles it can be used by *= selector then. The person who asked question should read reference which I provide about selectors.
The element's style attribute has no space and no semicolon. *= isn't going to find float: left;. But I covered that in my answer (which I deleted as David's is the most appropriate and most robust solution).
|
1
$('img[style*="float"][style*="left"]').addClass('alignleft');

http://jsfiddle.net/rzp79/

EDIT Well ok, you can't use it if you also have a border-left for example.

1 Comment

This will not do the trick. Just consider this case float:right and align:left. Does it make sense?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.