rozerro rozerro - 1 month ago 8
jQuery Question

A function does not work

A document contains script

$(document).ready(function() {

$(function reset(){
$("#select-result").empty().html(" ");
userInputSumm = 0;
userInput = [];
console.log('reser was executed!');
});

$("#reset").click(function() {
reset;
/* $("#select-result").empty().html(" ");
userInputSumm = 0;
userInput = []; */
});

});


and in page html

<p>
<a href="#" id="reset">Reset all</a>
</p>


The function reset executed on page load.
1)How can I prevent it from executed on page load?
2)When I press link
Reset all
the function
reset
does not work? Why?

Answer

Give this a shot

Remove the $( ) around your function. - This is what makes it execute automatically.

Then in the button put ()'s after the name reset() - This is how you call a function in Javascript

$(document).ready(function() {

    function reset(){
        $("#select-result").empty().html("&nbsp;");
        userInputSumm = 0;
        userInput = [];
        console.log('reser was executed!');
    };

    $("#reset").click(function() {
        reset();
        /* $("#select-result").empty().html("&nbsp;");
        userInputSumm = 0;
        userInput = []; */
    });

});