Pregunta

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>.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top