I have a table structure that looks like:
<table> <tr id="row1"> <td> <div>row 1 content1</div> </td> <td> <div>row 1 content2</div> </td> <td> <div>row 1 content3</div> </td> </tr> <tr id="row2"> <td> <div>row 2 content1</div> </td> <td> <div>row 2 content2</div> </td> <td> <div>row 2 content3</div> </td> </tr> <tr id="row3"> <td> <div>row 3 content1</div> </td> <td> <div>row 3 content2</div> </td> <td> <div>row 3 content3</div> </td> </tr> </table>
Using jQuery I am trying to select the DIV in the second cell of the third row. I tried the following (amongst other things):
var d = $('#row3').children(':eq(1)').children(':eq(0)');
What I get back is an array with a single element (the DIV I'm after) and I have to then access using d[0]. Why is jQuery returning a single element array, I thought using the selector above would return the DIV element directly?
@Shog9 - Duh...Ok a light just switched on in my brain, I get it now. Cheers.
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
The $() factory function Every jQuery selector start with thiis sign $(). This sign is known as the factory function. It uses the three basic building blocks while selecting an element in a given document.
If you prefer keeping a jQuery object, you may write instead:
$("selector").first().val()
jQuery always returns a set of elements. Sometimes, the set is empty. Sometimes, it contains only one element. The beauty of this is that you can write code to work the same way regardless of how many elements are matched:
$("selector").each(function() { this.style.backgroundColor = "red"; });
Fun!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With