Reputation: 3073
I already asked a similar question for buttons in tags, but I find that solution doesn't work using tags. So, if I am using a script that references jQuery Mobile I have the following line:
<button id="buttonAnswer1" data-inline="true">Click me</button>
How would I add a listener for when this button is clicked and lets say call the hello() function? i.e.
<button id="buttonAnswer1" data-inline="true">Click me</button>
<script>
function hello(){
console.log("hello world");
}
</script>
Upvotes: 0
Views: 1764
Reputation: 12437
Are you wanting to select all <button>
with data-line set to true
?
if so, try this: http://jsfiddle.net/M9pwp/2/
html:
<button id="buttonAnswer1" data-inline="true">Click me</button>
script:
$("button").click(function() {
if ($(this).attr('data-inline') == 'true')
hello();
});
function hello()
{
alert('hi');
}
if you only want a listener for this particular button, then you can change the script to:
$("#buttonAnswer1").click(function() {
hello();
});
function hello()
{
alert('hi');
}
Upvotes: 1
Reputation: 7375
Can you please refer the below code.
$("button#buttonAnswer1").on("click",hello);
Please try the above code in jsfiddle and let me know your concerns.
Upvotes: 1