jQuery attr(), prop() and val() function difference

When value attribute given

If the value attribute is found in the input tag/element, then no difference between either function attr(), prop() and val() used e.g.

<input type="text" name="pitribe" value="">

When value attribute not given

If the value attribute is omitted then these will be have like following.

<input type="text" name="pitribe">

val() returns empty string

val() function returns empty string i.e. ""

$('[name=pitribe]').val();

attr('value') returns null

attr('value') function returns null as attribute value is not found in the element/tag;

$('[name=pitribe]').attr('value');

prop('value') returns empty string

prop('value') function returns empty string i.e. "" because the attribute value is not defined but need to be defined

$('[name=pitribe]').prop('value');
Posted Status in Programming
Login InOR Register