.......">

How to set focus on a dropdown list named -javascript / jquery

I do not have a drop-down list as shown below.

<select name="dropname[0]">....</select>
<select name="dropname[1]">....</select>
<select name="dropname[2]">....</select>
<select name="dropname[3]">....</select>....

Now I want to set the focus to the second drop-down list. I tried these

document.getElementsByName('dropname')[1].focus();

returns this error

TypeError: document.getElementsByName(...)[1] is undefined

Thank you in advance

+4
source share
3 answers

Since you tagged your question with jQuery, try something like

$('select[name^="dropname"]').eq(1).focus();

// $('select[name^="dropname"]') < elements whos name starts with "dropname"
// .eq(1) < select one based on its index
// .focus(); < use jQuery focus method to set the focus
+5
source

Try using jquery focus like

$("#id2").focus();
+6
source

you can use

var i = 0 //or 1,2,3

document.getElementsByName('dropname['+i+']')[0].focus();

hope this helps you

+1
source

All Articles