Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting all empty text fields in Jquery

How can I find all text fields that have an empty value?

$(":text[value='']")  

gives a JavaScript error

I know I can do $(":text"), iterate through and return all fields with $(this).val()==''

I am looking for a cleaner method and using JQuery 1.3.1 It has to work if the element originally had a value when the page was loaded, and then the user cleared it. ($("#elem").attr('value') gives the original value in that place, though .val() works properly)

like image 219
questioner Avatar asked Mar 11 '09 16:03

questioner


People also ask

How check selector is empty in jQuery?

Method 1: Using the “:empty” selector: The element to be checked using is() method. The is() method is used to check if one of the selected elements matches to the selector element. This method can be used on this element to test if it is empty by using “:empty” selector.

How do you check all input field is not empty in jQuery?

Just use: $("input:empty"). length == 0; If it's zero, none are empty.


2 Answers

Latest Answer: Upgrade to 1.3.2

Here are various tests I ran via FireBug on http://docs.jquery.com/Downloading_jQuery

Different jQuery versions are switched in at page-load with special greasemonkey scripts.

>>> jQuery.prototype.jquery
"1.3.2"
>>> jQuery(":text[value='']")
[input#jq-primarySearch]
Unknown pseudo-class or pseudo-element 'text'.
>>> jQuery(":text[value=]").get()
[input#jq-primarySearch]

>>> jQuery.prototype.jquery
"1.3.1"
>>> jQuery(":text[value='']")
Syntax error, unrecognized expression: value='']
>>> jQuery(":text[value=]").get()
[input#jq-primarySearch]

>>> jQuery.prototype.jquery
"1.3"
>>> jQuery(":text[value='']");
Object length=1 prevObject=Object context=document
Unknown pseudo-class or pseudo-element 'text'.
[Break on this error] undefined
>>> jQuery(":text[value=]").get()
[input#jq-primarySearch]

Note that 1.3 and 1.3.2 handle it properly ( albeit with Firefox sending an error ) but they still get the node right.

Alternatively: you could use the :text[value=] notation, which appears to work everywhere I tried it. Its just a bit suspect thats all.

( Ignore my Previous rantings, they're all bollocks, not having a good day -_-)

like image 112
Kent Fredric Avatar answered Sep 22 '22 01:09

Kent Fredric


I've just tried this and worked fine for me:

$(":text[value=]")

I just removed single quotes in selector.

like image 37
Seb Avatar answered Sep 23 '22 01:09

Seb