Zala Raviraj Zala Raviraj - 1 year ago 58
Javascript Question

Call class in AJAX response text

I have div in which I am getting ajax response

<div id="demo"></div>

And in response I am getting three buttons with id and values

<button type="button" class="Model" onclick="ModelAppAll(<?php echo $i; ?>)">Approve All</button>
<button type="button" class="Model" onclick="ModelRejAll(<?php echo $i; ?>)">Reject All</button>

Now I want to call a function and get the
values in alert box like

function ModelAppReqId1(){

Or like this with jquery


In short I want work with AJAX response like actual DOM element. I am not able to find any class [model] in response text.
Please give me some ideas.

Answer Source

If you use jQuery to attach a delegated click event handler then you can achieve this by placing the $i variable from your PHP code in a data-* attribute on the button which you can then retrieve in the event handler. Try this:

<button type="button" class="model" data-i="<?php echo $i; ?>">Approve All</button>
<button type="button" class="model" data-i="<?php echo $i; ?>">Reject All</button>
$(document).on('click', '.model', function() {
    var i = $(this).data('i');
    // use i here...

Note that document is just used for sake of an example here. Ideally you should change that selector to the nearest parent element to the buttons which is available in the DOM on page load.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download