Awan
Awan

Reputation: 18560

Select an element based on two attributes

I have an element like this

<input type='select' class='MultiSelect' id='myId'>

Now I want to get value of a selectbox where it must have MultiSelect class and Also Must have myid id.

How to do this ?

Thanks

Upvotes: 0

Views: 382

Answers (2)

DhruvPathak
DhruvPathak

Reputation: 43235

JavaScript only solution, no jQuery : http://jsfiddle.net/XFCzN/ But see how beautiful it looks with jQuery.

<input type='select' class='OtherClass' id='myid' value='no' >
<input type='select' class='MultiSelect' id='myid' value='yes' >
<input type='select' class='MultiSelect' id='nomyid' value='no' >

Script :

var reqInput = getByTwo("INPUT","MultiSelect","myid");
if(reqInput != null)
  alert(reqInput.value);
else
  alert("No Match");

function getByTwo(tagName,myClass,myId)
{
var myInputs = document.getElementsByTagName(tagName);
for(var i=0;i< myInputs.length;i++)
{
    if((myInputs[i].getAttribute('class') == myClass) && (myInputs[i].id == myId))
     return myInputs[i];    
}

return null; 
 } 

Upvotes: 1

Mithun Sreedharan
Mithun Sreedharan

Reputation: 51264

with jQuery $('#myid.MultiSelect').val()

Upvotes: 6

Related Questions