I use this function to check for the presence of attributes:
var hasAttribute = function(el, attr) {
if ("hasAttribute" in el) {
return el.attributes[attr] != null;
}
else if ("outerHTML" in el) {
return (el.outerHTML
.replace(el.innerHTML, "")
.indexOf(" " + attr + "=") > -1);
}
};
The problem is when I set an input value using .val(x)
, I can get the value if I use .val()
but hasAttribute
function cannot detect it. This jsfiddle shows the problem (I expected that first alert say "input has a value" and second shows it, but in Chrome at least first alert says "input needs a value").
How to make .val(x)
work as expected? (Or how to make hasAttribute
detect these kind of values)