Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass the variable in jquery click event function

I want to Pass the variable with click event in jquery

var get=3; 
$('#edit').click(function(event){
alert('You are getting:' + get);
}

please help me

html

<input type='submit' name='action' id='edit' />
like image 409
knsmith Avatar asked Apr 28 '26 10:04

knsmith


2 Answers

You forgot the closing paranthesis.

Your javascript code should look like:

var get=3; 
$('#edit').click(function(event){
  alert('You are getting:' + get);
});
like image 95
Linostar Avatar answered Apr 30 '26 00:04

Linostar


You have to initialize click even once document is loaded. Also you have syntax error in your code.

Try this one:

$(document).ready(function() {
    var get=3;
    $('#edit').click(function(event){
        alert('You are getting:' + get);
    });
});

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var get=3;
        $('#edit').click(function(event){
            alert('You are getting:' + get);
        });
    });
    </script>
</head>
<body>
    <input type='submit' name='action' id='edit' />
</body>
</html>
like image 34
Shreejibawa Avatar answered Apr 29 '26 23:04

Shreejibawa