Take the 2-minute tour ×


Ignore the fact that this is a completely toy application. If there is NO time in which this is the right way to do things however, let me know.

So, I have a <form> with two <input> fields in it. I have an eventListener stuck to the <form> listening for a "submit" event. I then want to run a function which does some stuff to the number the user put in either input1 or input2. User types a number in say, input2, hits enter, and the function calls on this.id, but returns the id of the form, not the actual input box that is a child of the form.

What can I do?

EDIT: Should I look at all the child elements of form somehow and test each child to see if there's something in it, and then call alertGrade with the non-empty children as a parameter?


<form id="form">
  <input type="number" id="score1" value="" min="0" max="100">
  <input type="number" id="score2" value="" min="0" max="100">
  <input type="submit" value="Grade">


function alertGrade(elementID) {
  var score = document.getElementById(elementID).value;
  var grade = calculateGrade(score);
  alert("Score of " + score + ": " + grade);

function loaded() {
            function(event) {
                // this.id will show "form"

window.addEventListener("load", loaded, false);
share|improve this question
add comment

4 Answers

up vote 1 down vote accepted

You can use something like:

function loaded(){
    document.getElementById('form').addEventListener("submit", function(){
        for(var i=0, j=this.childNodes.length; i<j, i++){
            if(this.childNodes[i].value !== ""){
       alert("Please type something");
    }), false);

This will loop through each of the childNodes of the form and if it finds something has the value, it will send its value to alertGrade. If it does not find anything, it will alert the user to type something.

But a word of caution: Everytime the user clicks submit button, the page refreshes (atleast on my browser) and you may not get what you want.

share|improve this answer
Hey, nice. I believe your caution can be fixed by making function() into function(event) and adding event.preventDefault() in the body. –  Austin Yun Jul 16 '11 at 5:20
Well, then you solved my problem. –  Ankit Jul 16 '11 at 5:34
There seems to be a bug in my code due to which the user always get the alert message, even if he has typed something. I have updated the code with the fix. It was due the fact the I broke the for loop instead of returning the function –  Ankit Jul 16 '11 at 6:43
add comment

Have you tried event.srcElement? Because I did, and frustratingly it did not work as expected.

The simplest solution I could come up without using native JavaScript was to add a keypress listener to the input fields, and use that to catch and manually handle the return keypress, like:

<input type="number"
      onkeypress="return inputKeyPress(event, this);"
      min="0" max="100">

function inputKeyPress(evt, field) {

    if (evt.keyCode == 13) {
        return false;

    return true;

Example: http://jsfiddle.net/q8Eqn/

share|improve this answer
That's convoluted enough to lend credence to my theory that I'm just going about this completely the wrong way. –  Austin Yun Jul 16 '11 at 4:59
@Austin - Yes, have you considered just implementing your onsubmit listener so that it looks at both input fields, and then just picks whichever one has a value (or grades them both if both are filled in)? That would certainly be simpler. –  aroth Jul 16 '11 at 5:10
add comment

Like the other respondents, I also don't know if there's a way to do precisely what you want. But you can find out when an input field changes with the onchange event (slightly less involved than reading each keypress):

        function(event) {
        }, false);
share|improve this answer
add comment

As per my understanding you want value of score1 or score2.Then you should pass id "score1" and "score2" in your function call of alertGrade()

when you call alrtGrade(this.id).It is actually passing the id of form.


when you call getElementById,the id should be either "score1" or "score2"

share|improve this answer
Right, that's what I want, but I have no way of knowing whether score1 or score2 is the one that actually has any data in it. –  Austin Yun Jul 16 '11 at 4:54
For that,you have to check both like. var score1=document.getElementById('score1').value; var score2 =document.getElementBYId('score2').value; then apply your if else logic. –  WordsWorth Jul 16 '11 at 5:01
add comment

Your Answer


