views:

59

answers:

4

Hello everyone,

I am trying to find an element with a particular style and change its style to something else.

Here is the html element

<table style='width: 555px;'>
  <tr>
    <td>blablabla</td>
  </tr>
</table>

Now I am trying to find the table with width 555px and change it to 650px using jquery.

$('table[style*=width:555px]').css("width", "610px");

But this is not working. Can somebody spark an idea please?

NOTE: For some reason I cannot change the html.

+2  A: 

Well the obvious issue is that your element is a <table> and your jQuery selector is selecting all <div> s.

I'd imagine there's also an issue with whitespace too, as your HTML contains spacing within the style element but your selector doesn't. (I may be wrong here, not experienced with the E[a*=v] selector.)

chigley
sorry wrong code copied. My actual code block is $('table[style*=width:555px]').css("width", "610px");
Krishna
A: 

Is there a style selector in jQuery?

Alec
OP is not asking about selecting based on actual style but selecting based on the "style" attribute. Quite different from the linked question
Vincent Robert
Yes, and that's exactly what the css method in jQuery is for: "The .css() method is a convenient way to get a style property from the first matched element, ...". Check the answers from the link again.
Alec
`.css('width')` will get the width from any CSS, be it inline, stylesheet, whatever. It's not the same. Also, just linking another answer should be a comment, not an answer of your own.
Robert
Inline styles, as used in the OP's HTML, overwrite any CSS that might be there, so filtering on the .css('width') will give you the right results.
Alec
You're completely missing the point. He wants to get only inline. If I have elements that have the same width set in the stylesheet, they'll match `css('width')`. It's not a question of not meeting the requirements of finding his element, rather that it exceeds the scope of what he's looking for.
Robert
+2  A: 

Beware of spaces :)

And you should quote the value with " or '.

$('table[style*="width: 555px"]').css("width", "610px");

If it does not work in IE, you could try to remove the space? (totally untested!)

$('table[style*="width: 555px"],table[style*="width:555px"]')
    .css("width", "610px");
Vincent Robert
Partially Working, I mean working in firefox but not in IE :(
Krishna
+2  A: 
$('table').filter(function() {
    return $(this).css('width') == '555px';
}).css("width", "610px");
melhosseiny