user137348
user137348

Reputation: 10332

Jquery XML parsing

I have this XML

<items>
  <item validate="NotEmpty ValidEmail" class="xxx yyy zzz" type="input" name="Id" value="" label="Id" />
  <item validate="NotEmpty" type="input" name="NazovProjektu" value="" label="NazovProjektu" />
  <item type="input" name="Oz" value="" label="Oz" />

I want to get the value of the attribute name of each element with attribute class containing string yyy

Upvotes: 0

Views: 116

Answers (4)

Anurag
Anurag

Reputation: 141909

This might work, haven't tested it yet. Assuming the XML is already parsed and inside an object named xml.

$(xml).find("[class~='yyy']").each(function(i, element) {
    console.log($(element).attr("name"));
});

Upvotes: 1

John Hartsock
John Hartsock

Reputation: 86892

A class selector will get you the elements. The attr() function will get you the name of the element. Using each() on the jQuery Object will allow you to iterate through the element in the jQuery Object.

var yourXML = "...";
$("[class*='zzz']", yourXML).each(function() {
   alert($(this).attr("name"));
});

Upvotes: 1

Zain Shaikh
Zain Shaikh

Reputation: 6043

try following, it should help you out.

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "data.xml",
    dataType: "xml",
    success: function(xml)
        {
          //find every yyy classname and alert its name
          $(xml).find(".yyy").each(function()
          {
            alert($(this).attr('name'));
          });
        }
  });
});

Upvotes: 1

Hardeep
Hardeep

Reputation: 503

I believe you can do something like

$("item[class*=yyy]", xml).each(function() { 
    $(this).attr('name'); 
});

Upvotes: 1

Related Questions