1

Hi I am really new to Jquery and have a problem with my script , I want to access the 2nd 'heading' tag in my xml file using jquery . this is my script so for but what I want to do is assign a vairiable to the 2nd value of heading .

 $(document).ready(function()
        {

             $.ajax({
               type: "GET",
               url: "task.xml",
               dataType: "xml",
               success: displayXml
            });

        function  displayXml(data){

          $(data).find("tasks").each(function() {

          var heading = $(this).find("heading").text();

          });
        } 

        }); // doc ready 

this is my xml doc. What I am looking for is something like

name = $("heading",2).text(); the value being 'New Job'. Can anyone help me with this please ?

        <?xml version="1.0" encoding="utf-8"?>
        <tasks>

                <heading>Home </heading>

                <heading>New Job </heading>

                <heading>System </heading>

        </tasks>

2 Answers 2

2

Is this what are you looking for?

$("heading:eq(1)",data).text();

As you can see here in the doc http://api.jquery.com/jQuery/ jQuery() (same as $()) receives second parameter "context", in your case this will be data and jQuery will search in this context instead of your HTML/DOM.

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

Comments

1

insomniac's reply is, I believe, not quite correct, because :nth-child(2) returns every other selector, not just the second. The selector you need is :eq(1). This gets the second item to match the previous selector:

$(this).find('heading:eq(1)').text();

Note that eq uses 0-based indexing (i.e. the first item is 0, the second is 1, etc).

See the jQuery API for more information: http://api.jquery.com/eq-selector/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.