Posts tagged dom

Javascript – jQuery – Stopping Event Propagation – stopImmediatePropagation

0

Recently ran into another issue where I had two separate and de-coupled event handlers tied to the same dom element, specifically a form submit. However, the first event handler returned false if certain checkboxes were not checked (business rules), but the false was ignored by the second event handler, and still executed. After some searching I came across the jQuery stopImmediatePropagation function, which gave teeth back to the return false, by not executing any subsequent event handlers, and solved my issue.

Example:

$('#formId').submit(function(e) {

    var j=0;
    
    for(j; j<this.length; j++) {
        if (this[j].type === 'checkbox' && this[j].checked) {
            somethingSelected = true;
        }
    }

    if (!somethingSelected) {

        alert('Must select something to continue');
                
        e.stopImmediatePropagation();

        return false;
    }
            
    return true;
});

$('#formId').submit(function(e) {

    // If the previous event handler returned false, this event handler will not be executed
});

Javascript – jQuery – Property Submit of Object htmlformelement is not a Function

0

Recently ran into an issue while trying to add some jQuery logic to a form element, where the following error was being generated in the Javascript console every time I submitted the form: Uncaught TypeError: Property ‘submit’ of object # is not a function.

After some research, it was noted that there must be some other DOM element with the name or id of ‘submit’. So I viewed source and sure enough, found that the previous dev added the ‘name=”submit”‘ attribute to the “input” tag. After I changed the attribute to ‘name=”formButton”‘, everything worked as expected.

Go to Top