By Cracker0dks


2013-05-23 13:36:43 8 Comments

Try this at your JavaScript console (Chrome or something) with jQuery.

var jo = $("<xml required=\"true\" name=\"lol\"></xml>"); //test xml
jo.attr("required"); //returns "required" instead of true
jo.attr("name"); //returns correct "lol"
jo.prop("required"); //returns undefined
jo.prop("name"); //returns undefined

Anyone out there with a working solution to get the correct required value (true/false) of this xml?

.prop() only works with html like <input>.

1 comments

@Pointy 2013-05-23 13:44:56

The jQuery code has its own opinions on attributes and their nature, based on their names and semantics from the HTML world. That's why "required" gives you "required" as its attribute value.

You can try using .getAttribute() directly, though its results might be browser-dependent:

jo[0].getAttribute("required");

The .prop() function only works for HTML DOM elements because it relies on the browser creating objects with properties reflecting attribute values parsed from the source.

@Cracker0dks 2013-05-23 13:56:24

thanks jo[0].getAttribute("required"); works find for me :)

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] .prop() vs .attr()

23 Answered Questions

[SOLVED] Loading local JSON file

15 Answered Questions

[SOLVED] How do I parse XML in Python?

  • 2009-12-16 05:09:24
  • randombits
  • 1170210 View
  • 939 Score
  • 15 Answer
  • Tags:   python xml

13 Answered Questions

[SOLVED] How to define multiple CSS attributes in jQuery?

  • 2009-01-15 15:35:29
  • Edward Tanguay
  • 572812 View
  • 489 Score
  • 13 Answer
  • Tags:   jquery css

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

12 Answered Questions

[SOLVED] What does <![CDATA[]]> in XML mean?

  • 2010-05-06 20:20:50
  • dontWatchMyProfile
  • 1188080 View
  • 970 Score
  • 12 Answer
  • Tags:   xml cdata

4 Answered Questions

[SOLVED] jQuery attr vs prop?

12 Answered Questions

[SOLVED] Prevent RequireJS from Caching Required Scripts

4 Answered Questions

[SOLVED] Why jQuery 1.9+ attr() method not deprecated?

Sponsored Content