views:

583

answers:

3

I'm having some trouble with prototype changing the value of a hidden field.

Function:

function remove_fields (link) {
    $(link).next('input[type=hidden]').value = '';
    $(link).up(".open_hours").hide();
}

If I comment out the $(link).next('input[type=hidden]').value = ''; the hide function works. Trying to set the value gives me an error:

$(link).next("input[type=hidden]") is undefined

Here is my HTML around the function call:

    <div class="monday">

        <div class ="open_hours">
            <li><label for="location_monday">Monday</label>
            Open: 06:29PM - 
            Close: 04:21PM 
            <a href="#" onclick="remove_fields(this); return false;">remove</a></li>

            <li class="hidden optional" id="location_monday_open_input"><input id="location_monday_open" name="location[monday_open]" type="hidden" value="18:29:00" /></li>
            <li class="hidden optional" id="location_monday_close_input"><input class="close" id="location_monday_close" name="location[monday_close]" type="hidden" value="16:21:00" /></li>
  </div>    
</div>

Not sure what I'm doing wrong here? Thanks Guys!

A: 

You have tagged both jquery and prototype, in jquery you can try like this:

$(link).next('input[type=hidden]').val = '';
Sarfraz
ya I've tried that... for some reason that isn't working for me... the script will run without error (FireBug) but when I submit the form the older values are retained?
Nick Faraday
+1  A: 
rahul
$(link).closest('div.open_hours').find('input[type=hidden]').val(''); THIS WORKS! Let me ask you why does this not work: $(link).next('input[type=hidden]').val(''); ? Thanks Again! –
Nick Faraday
Please see my edit to the answer.Also I have removed the onclick from the HTML and put it inside the script.
rahul
Thanks for the response!
Nick Faraday
A: 

For prototype do this:

function remove_fields(link) {
  var e = link.next().identify();
  $(e).setValue("");
  $(e).up(".open_hours").hide();
}

You need to identify/return the elements id before you can set the value.

Darren