0

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>
1
  • The code you were given for your other question will bind a click handler to any element type, including <button>. (You don't have more than one element with the same id do you?) Commented Dec 10, 2012 at 5:15

2 Answers 2

1

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.

http://jsfiddle.net/

Sign up to request clarification or add additional context in comments.

Comments

1

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');   
}

Comments

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.