javascript - click a button based on div id and value with a script -


i have following situation. need simulate button click on off value button under div based on id

<div id="devicecontrols1"> <button value="off">off</button> <button value="on" class="active">on</button> </div>  <div id="devicecontrols2"> <button value="off">off</button> <button value="on" class="active">on</button> </div 

i know not valid code in other words id like:

 document.getelementbyid('devicecontrols2').getelementsbyvalue('off').click(); 

im not sure how use javascript obtain result

using javascript, try this:

var buttons = document.getelementbyid('devicecontrols2').getelementsbytagname('button'); (var = 0; < buttons.length; i++) {     if (buttons[i].value == 'off') {          buttons[i].click();     } } 

using jquery (since have tagged):

$('#devicecontrols2').children('button[value="off"]').click(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -