Get value of any attribute from XML data in JavaScript?

To get the value of any attribute from XML data in JavaScript, you can use jQuery's attr() method or native JavaScript methods like getAttribute(). This is useful when working with XML strings or DOM elements that contain XML data.

Using jQuery's attr() Method

The attr() method allows you to extract attribute values from XML elements wrapped in jQuery objects:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XML Attribute Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <script>
        var xmlString = '<Game id="101" details="Play the game and win the amount">'
            + '<gameName name="Cricket" content="/localhost/content/game/play/" />'
            + '<gameName name="Tennis" content="/localhost/game/all/scratch/" />'
            + '</Game>';
        
        // Get the details attribute from the Game element
        var gameDetails = $(xmlString).attr("details");
        console.log("Game Details: " + gameDetails);
        
        // Get the id attribute
        var gameId = $(xmlString).attr("id");
        console.log("Game ID: " + gameId);
        
        // Get attributes from nested elements
        var cricketContent = $(xmlString).find('gameName[name="Cricket"]').attr("content");
        console.log("Cricket Content: " + cricketContent);
    </script>
</body>
</html>
Game Details: Play the game and win the amount
Game ID: 101
Cricket Content: /localhost/content/game/play/

Using Native JavaScript DOM Parser

For a jQuery-free approach, you can use the native DOMParser to parse XML and extract attributes:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XML Parser Example</title>
</head>
<body>
    <script>
        var xmlString = '<Game id="101" details="Play the game and win the amount">'
            + '<gameName name="Cricket" content="/localhost/content/game/play/" />'
            + '<gameName name="Tennis" content="/localhost/game/all/scratch/" />'
            + '</Game>';
        
        // Parse the XML string
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(xmlString, "text/xml");
        
        // Get the root element
        var gameElement = xmlDoc.documentElement;
        
        // Extract attributes using getAttribute()
        var gameDetails = gameElement.getAttribute("details");
        var gameId = gameElement.getAttribute("id");
        
        console.log("Game Details: " + gameDetails);
        console.log("Game ID: " + gameId);
        
        // Get attributes from child elements
        var gameNames = xmlDoc.getElementsByTagName("gameName");
        for (let i = 0; i < gameNames.length; i++) {
            console.log("Game " + (i+1) + " Name: " + gameNames[i].getAttribute("name"));
            console.log("Game " + (i+1) + " Content: " + gameNames[i].getAttribute("content"));
        }
    </script>
</body>
</html>
Game Details: Play the game and win the amount
Game ID: 101
Game 1 Name: Cricket
Game 1 Content: /localhost/content/game/play/
Game 2 Name: Tennis
Game 2 Content: /localhost/game/all/scratch/

Comparison

Method Dependencies Browser Support Use Case
jQuery attr() Requires jQuery All browsers with jQuery When jQuery is already in use
Native DOMParser None Modern browsers Lightweight, no external dependencies

Key Points

  • Use $(xmlString).attr("attributeName") with jQuery for simple attribute extraction
  • Use find() method to target specific nested elements
  • Native DOMParser provides XML parsing without external dependencies
  • Always handle potential parsing errors when working with dynamic XML data

Conclusion

Both jQuery's attr() method and native JavaScript's DOMParser effectively extract XML attribute values. Choose jQuery for existing jQuery projects, or use native methods for lightweight, dependency-free solutions.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements