3

I want to find a closest class of the given object. My markup looks like this:

<div class="table_settings">

          <div style="float:left;">
              <div class="stats_options" id="yearSelector" style="width:140px;">
                  <div style="float:left"><span class="optionValue"></span></div>
                  <div style="float:right; margin-top:11px;"><img src="../images/arrow_down.png" /></div>
                  <div class="clear"></div>
              </div>

              <div id="yearSelectorOptions" class="option_list" style="width:160px; display:none; margin-top:0px;">
                  <ul>
                      <li><input type="radio" name="year" value="2" style="display:none;" alt="Winst en verlies" checked="checked" />Winst en verlies</li>
                      <li><input type="radio" name="year" value="1" style="display:none;" alt="Eindbalans" />Eindbalans</li>
                  </ul>
              </div>
          </div></div>

The jquery looks like this:

$(".option_list ul li").click(function() 
{


    $(this).children('form input[type="radio"]').prop('checked', true);

    value = $(this).children('input[type="radio"]:checked').attr('alt');
    $(this).parents('.table_settings').children('.optionValue').text(value); }

What I want is when I click on the [li] that the value of the clicked [li] gets into the class optionValue.

I want to duplicate this list so it have to work for more lists on one page.

I hope someone can help me.

Thanks in advance!

Leon van der Veen
  • 1,652
  • 11
  • 42
  • 60

2 Answers2

7
//select all the `<li>` elements that are the children of the `<ul>` element that is the child of the `.options_list` element
$(".option_list").children('ul').children().click(function() {

    //select the first ancestor element with the `.table_settings` class,
    //then select the `.optionValue` element that is a descendant of the `.table_settings` element,
    //then set it's text to that of the input element inside the `<li>` element clicked
    $(this).closest('.table_settings').find('.optionValue').text($(this).children().val());
});

Here is a demo: http://jsfiddle.net/rpVxV/

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • Jasper, Thanks for your answer but it didnt work properly. I use more of those lists on the page and when I click on one of the lists I get the value of the clicked list on all my lists... – Leon van der Veen Feb 22 '12 at 22:08
  • You will have to post more complete HTML for me to diagnose where it goes wrong. But you most likely need to give a class to the `
    ` elements and use it as the `.closest()` selector rather than `.table_settings`.
    – Jasper Feb 22 '12 at 22:10
  • I'm sorry, i forgot to close the of table_settings. So your solutions worked! Thanks – Leon van der Veen Feb 22 '12 at 22:17
1

Try this:

$('.option_list')
.find('li')
.click(function () {
    var $radio = $(this).children(':radio'),
        value = $radio.attr('alt'), // Or `$(this).text()` ...
    $(this)
    .parents('.table_settings')
    .find('.optionValue')
    .text(value);
});

Your code is basically not working because of this line:

//`.optionValue` is not IMMEDIATE 
// children of `.table_settings`
// so it won't work
$(this).parents('.table_settings').children('.optionValue').text(value); }
elclanrs
  • 92,861
  • 21
  • 134
  • 171