AFS
AFS

Reputation: 317

Jquery on click function called when the page is loaded

I'm trying to add a function to a button but the function is called when the page is loaded when the button is clicked, getMemberByName is on another js file and works correctly

<input type="button" value="miembro" id="boton2" />

<script>
    $("#boton2").click(getMemberByName('nombre1','apellido1'));
</script>

Upvotes: 1

Views: 45

Answers (4)

foebu
foebu

Reputation: 1415

If this can help someone, as @Moosman says, you need to wrap what you want to do in a function and you must not call. Example:

<script>
    $("#boton2").on("click", function(){getMemberByName('nombre1','apellido1')});
</script>

OR:

<script>
    $("#boton2").on("click", myfunction);

    function myfunction() {
        getMemberByName('nombre1','apellido1')
        }
</script>

Watch out to not call the function $("#boton2").on("click", myfunction()); because this will call the function just when you load the page and not after, for my experience.

Upvotes: 0

Mooseman
Mooseman

Reputation: 18891

You need to wrap them in functions:

<script>
    $(document).ready(function(){
        $("#boton2").click(function(){
            getMemberByName('nombre1','apellido1');
        });
    });
</script>

Docs: http://api.jquery.com/click/

Upvotes: 2

Salil Dabholkar
Salil Dabholkar

Reputation: 622

Try this instead:

$("#boton2").click(function() {
    getMemberByName('nombre1','apellido1');
});

Upvotes: 0

geevee
geevee

Reputation: 5451

here is the proper way to bind such event with jQuery:

<script>
$(function(){
    $('#boton2').click(function(){
        getMEmberByName('nombre1', 'apellido1');
    });
});
</script>

hope that helps.

Upvotes: 2

Related Questions