0

So, I'm using jQuery with PHP, and, I am echoing out a button:

echo '<input type="button" id="button" value="Info" style="float: right;"/>';

Pretty simple, I tried using "\" for the quotations, but, that didn't make any difference (for those who will point it out.

Anyway, the jQuery portion is pretty simple:

        $("#button").click(function() {
        alert("Clicked");
    });

Within the $(document).ready(function(){});

The echo is passed through an ajax call. If that makes any difference, and.

I do not get the alert. So if anyone can pinpoint the solution that would be great.

6
  • 1
    It doesn't matter who generated the code, but it matters what actually has been generated. Have you seen the final html in your browser? Commented Feb 29, 2012 at 22:34
  • debug: view source and fire bug Commented Feb 29, 2012 at 22:35
  • Does it work if you just make an html file with only those two elements. It's probably something else that's causing the problem. Also what browser did you test wtih? Commented Feb 29, 2012 at 22:35
  • 1
    <input type="button" id="button" value="Info" style="float: right; "/> - is what was generated according to firebug. There are multiple buttons mind you. Should it be kept as a class or id? Commented Feb 29, 2012 at 22:36
  • If you're loading the html from an ajax, you need to bind the event with live instead of click like @ken has done below in his answer. The element doesn't exist when the event is bound, so it doesn't work. Commented Feb 29, 2012 at 22:39

2 Answers 2

3
echo '<input type="button" id="button" value="Info" style="float: right;"/>';

$("input#button").live('click',function() {
        alert("Clicked");
});
Sign up to request clarification or add additional context in comments.

1 Comment

Worked, thanks, now, I just need to figure out parent classes, and, get the parent ID. That is something I can figure out on my own.
0

Try using " instead of ' for the quotes on the outside as PHP does not evaluate things inside single quotes.

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.