tags:

views:

78

answers:

4

I am unable to get length of textbox

$('input[id$=textinput1]').val().length returns nothing

I cannot use $('#textinput1').val().length - although this works

Update: My Element looks like this

<input id="textinput1" type="text" />
+2  A: 

Give it a name then (it should have one anyway), e.g. name="textinput1", and then use

$("input[name='textinput1']").val().length
Zed
Can I do it with ID instead of name?
Satya
Yes you can but you said you cannot use it.
Zed
A: 

This is probably because the query returns more than one element. If that is the case, then go through each one and get the length of the text in each textbox, or get the textbox you want and get the length of the text in it.

Marius
How can it return more than one element? I have only one textbox and am using id$=textinput1 to get the textbox I want
Satya
What is the element's full ID attribute value? It ends with 'textinput1' ?
meder
yes i have updated the question to reflect that
Satya
A: 
var el = $('input[id$=mode]');

if ( el.length ) {
    alert( $(el).val().length )
}

Remember $= denotes it ends with 'mode'

meder
this does not work..please did you test it?
Satya
Ok it should be it alert( $(el).val().length )
Satya
or $(el[0]).val().length, if the selector returns more than one element.
Marius
A: 

Why are you using 'id$=textinput1', rather than 'id=textinput1'? The first expression checks for an id that ends in 'textinput1', whereas you need to check for an exact value, as far as I can see, so you should be using the second expression.

If you have another element whose id ends with 'textinput1', then this will explain why the query is returning more than one element.

I also echo @piquadrat's question - why can you use "$('#textinput1').val().length"?

belugabob