0

Please refer below HTML

<div class="Icon">
                <span>
                        <img src=""/>
                    </span>

            </div>

I am adding class "bootstrap-modal" dynamically to above IMG tag when it is clicked.

$(".Icon img").on("click", function (e) {
            var data="some object";     
            $(this).addClass("bootstrap-modal");
    });

Then i have one more click event for bootstrap-modal class like below

$(document).on('click',"bootstrap-modal" function (e,data) {

//here i need to get data object 
});

how to pass data object from img click event to bootstrap-modal click event?

That means I need to get some values from previous click handler ? How can I do this ?

0

1 Answer 1

3

Save it in data:

$('.Icon img').on('click', function(e) {
    var data = 'some object';
    $(this).addClass("bootstrap-modal").data('test', data);
});

$('.Icon').on('click', '.bootstrap-modal', function(e, data) {
    var data = $(this).data('test');
});

FYI: Since you use the newest version of jQuery, you'd better use on method with event delegation instead of deprecated live. Here .Icon is supposed to be a static parent element of .bootstrap-modal.

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

6 Comments

If he has .on in one event handler he obviously has .on available in the other
@VisioN Thanks . i dont want to use data-attribute concept.because it will add attribute to that element. i don't want this behavior? any other way ?
@mplungjan :D haven't noticed that! I will update the answer then.
@SivaRajini Who said that .data() method will add the attribute to the element? It will not.
@SivaRajini Forget about data attributes. .data() method can set any data for any element internally. You may store arrays, objects, strings, whatever.
|

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.