0

Using a script that reference jQuery Mobile I have the following line:

<a href="#" id="buttonAnswer1" data-role="button" data-inline="true" >check</a>

How would I add a listener for when this button is clicked and lets say call the hello() function? i.e.

<a href="#" id="buttonAnswer1" data-role="button" data-inline="true">check</a>

<script>
function hello(){
  console.log("hello world");
}
</script>

1 Answer 1

1

This isn't really any different then regular jQuery, that is you can use .on to bind an event handler

For example, using .on with event delegation

$(document).on('click', '#buttonAnwser1', hello);

Or you can bind it directly

$('#buttonAnswer1').on('click', hello);
Sign up to request clarification or add additional context in comments.

2 Comments

Note that the second method needs to be included in a document ready handler and/or placed in a script block that appears after the element in question - many people put their script at the end of the body. Having said that, the second method would be my first choice for a static element selected by id.
document ready should not be used with jQuery Mobile and will cause issues with ajax based loading. stackoverflow.com/q/14236170/64262

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.