samedi 28 février 2015

Single Line/Non-Loop Javascript method to find item with specific text then invoke a click


I'm wanting to be able to click on an input that has no ID thats inside a LABEL that has no ID either.


Here is sample HTML...



<label>
<input type="radio" />
item1
</label>
<label>
<input type="radio" />
item2
</label>
<label>
<input type="radio" />
item3
</label>


now using ONLY Javascript. How could I go about just invoking a click on the item2 in this example?


I know that I could prolly do a FOR loop through all the document.getElementsByTag("label") then check the text of each one and then probably invoke a click after i've found th specific label...


BUT i was wondering if it was posssible to do this in a very simplified method instead like using document.querySelectorAll("label") or something to this effect? like is there noting that would allow me to do like a uh...



document.querySelectorAll("label[text='item2']").click()


Nothing like this or anything possible?


EDIT #1 - based off people stating its impossible for single selectors


If its not possible to do it using a querySelectorAll and you would have to use a For-Loop/While then how would you do this in a single line?


I know sometimes you can do stuff like...



javascript:(function() { document.getElementById('LABELID').value+='SETVALUE'; document.querySelectorAll("Button[type='submit']")[0].click();})()


Is it possible to do this search and click in a single line also?





Aucun commentaire:

Enregistrer un commentaire