jquery - Select element by and classname in javascript -
i have onchange event needs dynamically select id , add preset classname pass function.
onchange = "show(document.getelementbyid(this.value). {select class here? } );"
the equivalent in jquery
$('#'+this.value+'.myclassname').function();
so how select id+classname in javascript? know i'm being dense.
you need use classname? ids should unique.
var element = document.getelementbyid('myid');
or element
parent , want child class
var elements = element.getelementsbytagname('*'); var newelements = []; (var = 0, length = elements.length; < length; i++) { if ((' ' + elements[i].classname + ' ').indexof(' yourclassname ') > -1) { newelements.push(elements[i]); } }
this code walks through children, , uses indexof()
test presence of class. if finds it, pushes onto new array.
Comments
Post a Comment