2

i have this HTML code, how do i call a javascript function without making changes to my HTML file.

<tr>
     <td><div id="number1">1</div></td>
     <td><div>+</div></td>
     <td><div id="number2">2</div></td>
     <td><div>=</div></td>
     <td><input type="text"></input></td>
     <td><input type="button" value="Check"></input></td>
</tr>

tried to use this javascript code but didn't work

var button = document.getElementsByTagName("input");
button.onclick = function(){ alert('hello!'); };
1
  • I updated my answer,Please check it Commented Feb 24, 2015 at 7:12

5 Answers 5

1

Try this

  var button = document.getElementsByTagName("input");  
  button[1].onclick = function(){ button[0].value=3; };  
  1. we should get all input element, result is 2 input element. first is text, second is button
  2. then we should use second (button) button[1]...on click button we call function and in text input write value 3 (1+2=3)
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to SO. If you could please edit your answer and explain what the code you're showing does, and why/how that code answers the question, it could really help.
0

document.getElementsByTagName("input"); gives you list of inputs and not just a button.

Here, button.onclick = function(){ alert('hello!'); }; you are trying to attach click handler to the list itself which is not valid. You have to bind the handler one by one.

Edited:

Using document.querySelector():

//gets the input where type is button and value is check
var btn = document.querySelector('input[type=button][value=Check]');
//add click handler
btn.onclick = function() {
  alert('You clicked !');
};
<input type="button" value="Check"></input>

Using document.getElementsByTagName():

var inputs = document.getElementsByTagName('input'); //get all inputs
var i = 0;
var btn; //temp for button
var t //temp
while (t = inputs[i++]) { //loop in all inputs for correct button
  //verify if type is `button` and value is `check`
  if (t.type.match(/button/i) && t.value.match(/check/i)) {
    btn = t;
    break;
  }
}
if (btn) { //got the button
  btn.onclick = function() {
    alert('You click me !');
  };
}
<input type="button" value="Not me"></input>
<input type="text"></input>
<input type="button" value="Check"></input>

1 Comment

" how do i call a javascript function without making changes to my HTML file." this is his question,read
0

The function getElementsByTagName returns all input elements in the DOM, you need to check if the input is of type button like so:

var inputs = document.getElementsByTagName("input");

for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type === "button") {
        inputs[i].onclick = function () {
            alert("hello!");
        }
    }
}

Here is a fiddle: http://jsfiddle.net/hL2kavzs/

Comments

0

Try this...

document.getElementsByTagName("input").addEventListener("click",   function(){
    alert('hello');});

Comments

0

Try this using querySelectorAll()

 document.querySelector('input[type="button"]').addEventListener('click', function() { alert('hello'); });
<table>
<tr>
     <td><div id="number1">1</div></td>
     <td><div>+</div></td>
     <td><div id="number2">2</div></td>
     <td><div>=</div></td>
     <td><input type="text" /> </td>
     <td><input type="button" value="Check" /></td>
</tr>
</table>    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.