1

I have a list of items, and all of them will have a "click here for further info" link. With this link, I want to open a modal popup and display the details inside it. My problem is, how can I pass the id to the relevant modal popup.

At the moment I have the following code :-

Every item in the list will have the following:-

<a href="#" class="modal_link" data-id="@item.ExpId">Click here for more info.</a>

and in my jquery I have the following :-

    var id = $(".modal_link").attr("data-id");
alert(id);

$(document).ready(function () {
    $('.modal_block').click(function (e) {
        $('#tn_select').empty();
        $('.modal_part').hide();
    });

    $('.modal_link').click(function (e) {
        $('.modal_part').show();
        var context = $('#tn_select').load('/Experience/ShowExpDetail?id=' + id, function () {
            initSelect(context);
        });
        e.preventDefault();
        return false;
    });

});

however the id is always undefined.

How can I pass this var?

Thanks for your help and time

1
  • You have place that piece of code in document.ready Commented Jan 8, 2013 at 9:35

3 Answers 3

2

Since you want the id of each link to be supplied when it's clicked, you need to use a self-reference, which you get from this, like so:

$('.modal_link').on('click', function (e) {
    e.preventDefault();
    $('.modal_part').show();

    var id = $(this).attr('data-id');
    var context = $('#tn_select').load('/Experience/ShowExpDetail?id=' + id, function () {
        initSelect(context);
    });
});

Your load() call doesn't feel entirely right to me, but I'd have to see the rest of the script to know...

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

1 Comment

the load() is done through a PartialView inside the controller
0

Just move these lines var id = $(".modal_link").attr("data-id"); alert(id); inside $(document).ready(function () {

8 Comments

Inside the click event, actually, but close.
Does it matters? Anyway result will be the same :)
Yes, it does matter, and no, the results aren't the same.
you are right Morpheus, I though it did not matter if it was inside or not
@Morpheus There is more than one .modal_link in the page, and this would fix the id value at the id of the first .modal_link.
|
0
 $('.modal_link').click(function (e) {

 alert($(this).attr('id'));
        $('.modal_part').show();
        var context = $('#tn_select').load('/Experience/ShowExpDetail?id=' + id, function () {
            initSelect(context);
        });
        e.preventDefault();
        return false;
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.