Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find element by style selector in jquery

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.

like image 280
Krishna Avatar asked Oct 01 '10 18:10

Krishna


People also ask

Can you use CSS selectors in jQuery for selecting elements?

Projects In JavaScript & JQueryjQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element.

How can I select an element by name with jQuery?

How to select an element by name with jQuery? The JavaScript getElementsByName() method can be used to select the required element and this can be passed to a jQuery function to use it further as a jQuery object.

How will you select all the div elements on the page using jQuery?

The jQuery syntax is a little simpler, but not much more: $("div") is the jQuery equivalent to document. getElementsByTagName("div") . Here is the API documentation on jQuery's element selector: “Description: Selects all elements with the given tag name.

How do you select the first element with the selector statement?

The :first selector selects the first element. Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent). This is mostly used together with another selector to select the first element in a group (like in the example above).


1 Answers

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");
like image 84
Vincent Robert Avatar answered Oct 06 '22 20:10

Vincent Robert