Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an EventListener to a form, running a function on submit on whatever child <input> in the form actually called the submit

http://jsfiddle.net/twG6R/8/

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?

HTML:

<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">
</form>

JavaScript:

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

function loaded() {
    document.getElementById("form").addEventListener("submit",
            function(event) {
                event.preventDefault();
                // this.id will show "form"
                alert(this.id);
                alertGrade(this.id);},
                false);
}

window.addEventListener("load", loaded, false);
like image 624
Austin Yun Avatar asked Jul 16 '11 04:07

Austin Yun


3 Answers

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 !== "") {
                alertGrade(this.childNodes[i].id);
                return;
            }
        }
        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.

like image 81
Ankit Avatar answered Nov 17 '22 23:11

Ankit


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);"
      id="score1"
      value=""
      min="0" max="100">

function inputKeyPress(evt, field) {
    var evt  = (evt) ? evt : ((event) ? event : null);

    if (evt.keyCode == 13) {
        alertGrade(field.id);
        return false;
    }

    return true;
}

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

like image 42
aroth Avatar answered Nov 18 '22 01:11

aroth


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):

document.getElementById("score1").addEventListener("change",
        function(event) {
            alert(this.id);
        }, false);
like image 1
bbg Avatar answered Nov 17 '22 23:11

bbg