Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery to traverse the div and get its span details

Tags:

jquery

I have a html page as below:

<div id="test1">
    <table>
        <tr>
            <td><span id="234">ABKO</span></td>
        </tr>
        <tr>
            <td><span id="1234">ABKO2</span></td>
        </tr>
        <tr>
            <td><span id="2634">ABKO3</span></td>
        </tr>
    </table>    
</div>
<div id="test2">
    <table>
        <tr>
            <td><span id="233">ABKOw</span></td>
        </tr>
        <tr>
            <td><span id="1236">ABKOc</span></td>
        </tr>
        <tr>
            <td><span id="2635">ABKOv</span></td>
        </tr>
    </table>    
</div>

How can i get all the span details between using jQuery? eg if i consider first div=test1 then i want all the soan with their ID and text as div -> test1 Span => id 233 && ABKO Span => id 1234 && ABKO2 Span => id 22634 && ABKO3

like image 879
user367134 Avatar asked Aug 04 '10 17:08

user367134


People also ask

How do you traverse the Dom in jQuery?

Traversing the DOM. jQuery provides a variety of methods that allow us to traverse the DOM. The largest category of traversal methods are tree-traversal. The next chapters will show us how to travel up, down and sideways in the DOM tree.

How to find the <span> elements within a division using JavaScript?

Approach: We will first select the division inside which we are going to find the <span> elements. This can be done using by selecting the element by its ID using the getElementById () method. We will next get all the elements that are of the specified type that are contained in this division.

What is traversing in jQuery?

With jQuery traversing, you can easily move up (ancestors), down (descendants) and sideways (siblings) in the tree, starting from the selected (current) element. This movement is called traversing - or moving through - the DOM tree.

How to get the text content of a span in JavaScript?

This can be done by using the getElementsByTagName () method on the division we have found in the previous step. We can then get the text content inside it by looping through all the span elements and display it as a list. The below examples demonstrate this approach. Attention reader!


2 Answers

You can iterate through all the <span> elements like so:

$('div span').each(function(){
    var $span = $(this);

    var divId = $span.closest('div').attr('id');
    var spanId = $span.attr('id');
    var spanTxt = $span.text()

    // do something with the above
});

You can see it in action here.

What this is doing is building a list of span elements that are nested in a div (CSS selector div span). It then iterates over this list using the .each() method.

Within each iteration $(this) refers to the matched span element. We capture a reference in the $span variable so we don't keep recreating the same jQuery object.

We then use the .closest() method to find the span's first ancestor that matches the given selector (div). This gets our parent div.

Finally .attr() and .text() gets the ID attribute and text values .

like image 151
Pat Avatar answered Oct 12 '22 10:10

Pat


You can find an element by Id with something like $('#test1'). You can find elements by tag with $('span') or $('div').

If you want to find all the spans belonging to "test1":

$('#test1').find('span')

or

$('#test1 span')

You can iterate over a collection of elements with each():

$('#test1').find('span').each(function() { 
    var id = this.id;
    var value = $(this).html();
    // do whatever...
});

Hopefully you can work the rest out yourself from this.

like image 28
fearofawhackplanet Avatar answered Oct 12 '22 10:10

fearofawhackplanet