0

i have a jquery script:

    $.get('php/username.php', { username: userName },
    function(data){
        var get_back = data;
        alert(get_back);
    });

what happes is that if i run it in firebug console it works, but if i add it inside a click function, it doesnt:

$(document).ready(function(){
$("#btnLogout").click(function () {
    $.get('php/username.php', { username: userName },
    function(data){
        var get_back = data;
        alert(get_back);
    });
});
});

html

<p><input type="submit" name="btnLogout" id="btnLogout"  value="Logout" class="page"/></p>

i don't understand.

thanks

3
  • Can you post your HTML markup? And make sure you attach the click handler once the DOM is ready - perhaps in $(document).ready(); Commented Apr 8, 2011 at 22:47
  • updated. and i use $(document).ready() Commented Apr 8, 2011 at 22:50
  • Please see my updated answer - your type should be changed from submit to button unless you have compelling reason not to. Commented Apr 8, 2011 at 22:53

2 Answers 2

2

Update

You're using an input of type submit - change it to type="button" instead - a submit button already has default functionality (when inside of a <form>) that you will need to stop by using event.preventDefault() and event.stopPropogation() - see below:


$(document).ready(function(){
    $("#btnLogout").click(function (e) {
        e.preventDefault(); //stop the submit event for the submit button
        $.get('php/get_username.php', { username: userName },
        function(data){
            var get_back = data;
            alert(get_back);
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

$(document).ready(function(){ is there. still no difference
2

Try $("#btnLogout").submit(function(){...}) instead

2 Comments

.live doesn't seem to make a difference
Since input is a submit, Try the submit() event handler. Check updated answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.