0

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" /> 
1
  • 2
    why can't you use "$('#textinput1').val().length"? It's the right solution. Commented Sep 21, 2009 at 7:07

4 Answers 4

2

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

$("input[name='textinput1']").val().length 
Sign up to request clarification or add additional context in comments.

2 Comments

Can I do it with ID instead of name?
Yes you can but you said you cannot use it.
0
var el = $('input[id$=mode]'); if ( el.length ) { alert( $(el).val().length ) } 

Remember $= denotes it ends with 'mode'

2 Comments

Ok it should be it alert( $(el).val().length )
or $(el[0]).val().length, if the selector returns more than one element.
0

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.

2 Comments

How can it return more than one element? I have only one textbox and am using id$=textinput1 to get the textbox I want
What is the element's full ID attribute value? It ends with 'textinput1' ?
0

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"?

Comments