1

I'm not even sure I'm asking or going about this the right way but it's probably easier if I just show it. Basically, I'm trying to have run an attr of each div as a parameter through a function and have it return a different result based on that div's attr.

The example, as you can see, is a group of dropdowns that appear when you click on a link in the container div. If you make a selection it saves that as a attr in the parent div. The problem arises when you click out, then back in on the container ... instead of reshowing each dropdown with the appropriate default or selection showing, it just mirrors the result of the a next to it.

http://jsfiddle.net/nosfan1019/b7F6x/5/

TIA

3
  • 1
    Try to add some comments to your example, it's hard to read, imho Commented Aug 2, 2012 at 0:28
  • 1
    Try renaming your variables to something more legible than asdf and eeee. Commented Aug 2, 2012 at 0:32
  • Sorry about that. Went through and added both comments and better variables. jsfiddle.net/nosfan1019/b7F6x/5 Commented Aug 2, 2012 at 0:41

2 Answers 2

2

I inserted some console.log() statements to see what was happening with your various jQuery selectors. I observe the following:

  • when I click in the first "click" node, _container is "top one"
  • thus in your iteration of the three divs, you select both divs with class 'dd' contained in the div with class 'top one'
  • the parameters _attr and _parent that you pass to your function select() are the same for each node that is processed, giving the same result for both 'dd' boxes.

I think you want to change the selectors you use to locate the nodes to modify.

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

3 Comments

Thats what I was trying to describe in a sense. So its not a matter of passing some form of an array as a parameter, you don't think?
I don't think the problem is parameters and function calling, I think it is just selecting which objects you want to act on. If you comment out the line in the else: //$(this).find('.dd').empty(); does that give you the desired behavior? That else explicitly clears both divs in the other two "top" divs.
Not entirely, but that might have to work. The idea is that they hide or go to plain text when another part of the page is engaged. Oh well ... I appreciate the help
1

foo = foo.find('.dropdown-toggle').html(_new + '<b class="caret"></b>');

with this line you get two divs and hence you've change both values(in case the value was chosen from the droplist).

To restore selected values correctly:

function modified(_select) {
    console.log("modify");
    foo = $('#box').html();
    foo = $(_select).html(foo);
    // iterate on collection to restore selected value from selection tag;
    foo.filter("div[selection]").each(function(i, v){
        var selected = $(v).attr('selection');
        $(v).find('.dropdown-toggle').html(selected + '<b class="caret"></b>');                
    });                
}

Then, it's needed to be checked if any of parentDiv has [selection] attr:

if($(y).filter("div[selection]").length > 0){
    return modified(y);
}         

http://jsfiddle.net/b7F6x/50/

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.