0

I am using this code to loop through each ".accessoryrow" and then select "dialog" + counter and ".see-details" + counter. So first time when loop goes by it selects dialog1 class and see-details1 class; second time dialog2, see-details2 and so on. I think I am not correctly adding counter to the selector. Please correct me. Thank you

CODE:

  var counter = 1;
        $(function () {
        $(".accessoryrow").each(function() {
            $(".dialog" + counter).dialog({
                autoOpen: false,
                show: "blind",
                hide: "fade"
            });

            $(".see-details" + counter).click(function () {
                $(".dialog" + counter).dialog("open");
                return false;
            });
            counter++;
        });
3
  • Do you get any javascript errors? Commented Sep 23, 2011 at 17:00
  • No, and it seems like it's actually selecting correctly because if I don't select then I got extra text on the page. It's just that it doesn't show the dialog on see-details click Commented Sep 23, 2011 at 17:05
  • Is .see-details added to the DOM after the page loads? Commented Sep 23, 2011 at 17:23

2 Answers 2

1

The problem is that the $(".dialog" + counter).dialog("open"); line isn't getting evaluated until the link is clicked. Thus it's using the value of counter which is current as of then. A better way to do it would be to take out the counter altogether, and use jQuery selectors to select the correct .dialog.

Without the HTML, I can't say what it should look like, but you're going to want the click function to look like something along the lines of

 $(".see-details").click(function () {
        $(this).sibling(".dialog").dialog("open");
        return false;
    });

Of course, that assumes that the .dialog element is actually a sibling of .see-details. You'll need to traverse the tree a bit more if it isn't.

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

Comments

0

Try this (untested):

$(function () {
    $(".accessoryrow").each(function(index) {
        $(".dialog" + (index + 1)).dialog({
            autoOpen: false,
            show: "blind",
            hide: "fade"
        });

        $(".see-details" + (index + 1)).click(function () {
            $(".dialog" + (index + 1)).dialog("open");
            return false;
        });
    });

Index passes the loop number to the function. It starts from 0, where I think you need it to start at 1, so I've added 1 to each where it's used.

3 Comments

I'm pretty sure index isn't going to be visible to the inner function. Even if it were, the inner function only gets evaluated on click, so index will be the wrong value.
I realise this question is answered but the index is visible to the inner function. This in document.ready: $('.someClass').each(function(index){ $(this).click(function(){ alert(index); }); }); The alerts that come up on click in this case display the value of index for that element, 0 - N.
Hmm. I've never been able to get that to work before. I'm going to have to take another look at this the next time I need it. Clearly I've been doing something wrong.

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.