Reputation: 42818
I'm trying to check if an attribute exists on an input element. I created a hasAttr plugin as you see below to check if attr is undefined and false. Everything appears to be written correctly, but I'm not getting any response or errors.
HTML
<form id="form">
<div><input type="text" /></div>
<div class="selected"><input type="text" /></div>
<div><input type="text" /></div>
</form>
jQuery hasAttr plugin
$.fn.hasAttr = function(name) {
return this.attr(name) !== undefined && this.attr(name) !== false;
};
Function for passing attr name If attr exist then set value abc else alert false
function formeditvalues(attr) {
if ($('#form').find('.selected input').hasAttr(attr)) {
$('#form'.find('.selected input').val('abc');
} else {
alert('false');
}
}
Calling the function gives no response.
formeditvalues('type');
Check jsfiddle http://jsfiddle.net/KPczs/1/
Upvotes: 1
Views: 1256
Reputation: 630409
There is an error in the console, you're missing a )
, here:
$('#form'.find('.selected input').val('abc');
^
Fixing this to:
$('#form').find('.selected input').val('abc');
As an aside, false
is a valid attribute, for example disabled
and readonly
are examples of boolean attributes in the DOM, so your !== false
may not be a desired check. There's also the [attr]
(has-attribute` selector) you can use as well, like this:
$('#form .selected input['+attr+']').val('abc');
Upvotes: 1