views:

138

answers:

2

Basically what I'm trying to do is update the value attribute of a hidden input field contained within the selected element when the selectable() UI stops running.

If the element is selected, then the input's value should be the name attribute of that particular LI, whereas if the element is not selected, the value should be updated as empty.

HTML Sample:

<ul id="selector">
    <li class="networkicon shr-digg" name="shr-digg">
        <div></div>
        <label>Digg</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
    <li class="networkicon shr-reddit" name="shr-reddit">
        <div></div>
        <label>Reddit</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
    <li class="networkicon shr-newsvine" name="shr-newsvine">
        <div></div>
        <label>Newsvine</label>
        <input type="hidden" value="" name="bookmark[]" />
    </li>
</ul>

Script Sample:

$(function() {
    $("#selector").selectable({ 
        filter: 'li',
        selected: function(event, ui) {
            $(".ui-selected").each(function() {
                $(this).children('input').val($(this).attr('name'));
            });
        },
        unselected: function(event, ui) {
            $(".ui-selected").each(function() {
                $(this).children('input').val('');
            });
        }
    });
});
+1  A: 
$(this).children('input').attr("value", $(this).attr('name'));
...
$(this).children('input').attr("value", '');

Does that solve it ?

http://api.jquery.com/val/:

Description: Get the current value of the first element in the set of matched elements.

Bastiaan Linders
Yes, of course the simplest thing would cure the problem! hahaThanks.
Josh
+1  A: 

Your .each syntax is incorrect, you seem to be mixing up the syntax with $.each. It should be:

$(".ui-selected").each(function() {
    $(this).children('input').val($(this).attr('name'));
});
karim79
Yea, sorry... Forgot to remove that when posting the sample.
Josh