Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  143] [ 6]  / answers: 1 / hits: 16239  / 13 Years ago, fri, august 19, 2011, 12:00:00

I want to Simulate a button click via JavaScript using a button's value, not its id.



Here is the code using the button id



<input type=checkbox onClick=document.getElementById('theSubmitButton').click();>Check the box to simulate a button click
<br>
<input type=button name=theSubmitButton id=theSubmitButton value=Button onClick=alert('The button was clicked.');>


I've tried getElementByValue('Button') but it didn't work.


More From » button

 Answers
57
<script type=text/javascript>
function getButtonByValue(value) {
var els = document.getElementsByTagName('input');

for (var i = 0, length = els.length; i < length; i++) {
var el = els[i];

if (el.type.toLowerCase() == 'button' && el.value.toLowerCase() == value.toLowerCase()) {
return el;
break;
}
}
}
</script>
<input type=checkbox onClick=getButtonByValue('Button').click();>Check the box to simulate a button click
<br>
<input type=button name=theSubmitButton id=theSubmitButton value=Button onClick=alert('The button was clicked.');>


jsfiddle example


[#90530] Thursday, August 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcos

Total Points: 331
Total Questions: 106
Total Answers: 104

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
marcos questions
Thu, Feb 24, 22, 00:00, 2 Years ago
Sat, Apr 17, 21, 00:00, 3 Years ago
Mon, Feb 1, 21, 00:00, 3 Years ago
Tue, Jan 26, 21, 00:00, 3 Years ago
Wed, Jan 6, 21, 00:00, 3 Years ago
;