7

I am using jquery mobile click function, however, it is not working.

Here is an example of the button that I have, and it is contained within a grid:

<div class="ui-block-c"><a class="request" data-role="button" data-id="\"'+json[i].num+'\" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div>

jQuery function:

$('.request').on('click', function() {
    alert("hi");
});

How do I fix this?

2
  • 1
    It's working here Commented Sep 6, 2013 at 18:50
  • Are you sure that you bind when the element has already been loaded ? That's not the right way to use on. Commented Sep 6, 2013 at 18:51

2 Answers 2

17

It looks like you are adding this element dynamically, so you'll need to use a delegated event listener:

$(document).on('click', '.request', function() {
    alert("hi");
});

Also you have an issue with your escaped quotes not matching. I don't think those are necessary:

<div class="ui-block-c"><a class="request" data-role="button" data-id="'+json[i].num+'" data-type="3" data-icon="plus" data-iconpos="right">Test</a></div>
Sign up to request clarification or add additional context in comments.

5 Comments

this works, however, the formatting to the button is lost, do you know how to approach that?
the quotes are fine, because the html code is within quotes '', because it is later being appended by jquery
What formatting is lost? As for the quotes, your question had an odd number of quote characters, which isn't valid HTML and will cause you rendering issues. This may be the source of your formatting problems.
Your solution works, but why the other way of adding the event $('.request').on('click') won't work ? I had the same issue and would like to understand the cause.
The request element doesn't exist, so you can't bind an event to it. You might want to read up on event delegation to see how to solve the problem: learn.jquery.com/events/event-delegation
-4
$(.request).click(function(){
alert("hi")
});

5 Comments

Hi Jack, welcome to Stackoverflow! Please explain your answers with words also. Why you think this should be the answer. In case you gave a answer that does not work you can delete it or edit it. But nice with some words explaining your idea. Cheers!
In this case I don't think also changing to .click() would make a difference or be the solution to this question.
Wow! You guys are harsh, i was only trying to help :(
Jack, don't take it personally :)
This is a great community and the way to say I disagree is to downvote. You can always edit or delete your answer. And keep on! you are very welcome in SO

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.