Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus on dropdown by its name -javascript/jquery

I have a no of dropdown list as follows

<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 focus on second drop down list.I tried these

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

results this error

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

Thanks in advance

like image 992
manuthalasseril Avatar asked Oct 01 '13 12:10

manuthalasseril


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's focus method to set the focus
like image 113
Johan Avatar answered Oct 27 '22 00:10

Johan


Try using jquery focus like

$("#id2").focus();
like image 27
abcd Avatar answered Oct 26 '22 22:10

abcd


You can use

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

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

hope this will help you

like image 32
Pradeep Avatar answered Oct 27 '22 00:10

Pradeep