Selecting all empty text fields in Jquery
Asked Answered
K

6

7

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)

Kare answered 11/3, 2009 at 16:10 Comment(0)
K
11

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 -_-)

Kalisz answered 11/3, 2009 at 17:5 Comment(1)
So only 1.3.1 has this problem... ThanksKare
A
4

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

$(":text[value=]")

I just removed single quotes in selector.

Anlace answered 11/3, 2009 at 22:49 Comment(1)
This seems to work for me too. Although, I'm not sure if it is 100% equivalent. I'm afraid that it might act as $(":text[value]") in some situation.Strut
T
2

You were close. There's no assignment in jQuery selectors, so you only need a single '=':

$(":text[value='']")
Timbre answered 11/3, 2009 at 16:15 Comment(3)
sorry, question edited to remove the extra '='. That gives me an error using jquery 1.3.1Kare
Seems the issue is with passing an empty value to an attribute selector.Calisaya
I'm not sure what you're doing ngz, but this answer works for me. theres the potential though your empty values are getting removed from the dom :)Kalisz
C
1

This seems to work for me

$(":text:not([value])")
Calisaya answered 11/3, 2009 at 16:47 Comment(2)
this does not give a Javascript error, but if the element originally had a value and the user cleared it, it will not be selected.Kare
I had a feeling it may be problematic, as it only specifies the attribute and not the value.Calisaya
D
1

The following code does this:

  1. Searches the form for inputfields and textareas
  2. Checks if the text value is empty or not
  3. gives the textfield/textarea a black background-color to prove this works

    $('form').find("input[type=text], input[type=password], textarea").each(function(ev)
    {
    
    if(!$(this).val())
    {
       $(this).css("background-color","black");
    }
    
    });
    
Debbidebbie answered 26/9, 2012 at 16:23 Comment(0)
E
-2

Shot in the dark as I haven't tested it, but does this work:

$(":text:empty")
Epithet answered 11/3, 2009 at 16:34 Comment(1)
No, :empty will select all text fields, because it matches fields with no childrenKare

© 2022 - 2024 — McMap. All rights reserved.