开发者

Select an element based on two attributes

开发者 https://www.devze.com 2023-02-15 19:04 出处:网络
I have an element like this <input type=\'select\' class=\'MultiSelect\' id=\'myId\'> Now I want to get v开发者_如何学运维alue of a selectbox where it must have MultiSelect class and Also Mus

I have an element like this

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

Now I want to get v开发者_如何学运维alue of a selectbox where it must have MultiSelect class and Also Must have myid id.

How to do this ?

Thanks


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


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; 
 } 
0

精彩评论

暂无评论...
验证码 换一张
取 消