robert trudel
robert trudel

Reputation: 5749

Jquery hidden input select to to write value

I use Jquery typeahead

In the onClickAfter: function(node, a, item, event) event i do

node.closest('td').first('input[type=hidden]').attr('value',item.id);

A part of the html code

<td>
     <input type="hidden" name="carParams[2].id">
     <input type="hidden" name="carParams[2].idJoins">
     <div class="typeahead__container cancel">
             <div class="typeahead__field">
                 <span class="typeahead__query">
                     <span class="typeahead__cancel-button"></span><input name="carParams[2].name" class="form-control js-typeahead" type="search" placeholder="Type partial value" autocomplete="off">
                 </span>
             </div>
     <div class="typeahead__result"><ul class="typeahead__list"><li class="typeahead__item typeahead__group-carparam"><a href="javascript:;" data-group="carParam" data-index="0"><span class="typeahead__display"><strong>color value</strong></span></a></li></ul></div></div>
</td>

Problem is value field is not put a the right place...

<td value="45">

would like to put it to <input type="hidden" name="carParams[2].id">

Upvotes: 0

Views: 44

Answers (2)

Huangism
Huangism

Reputation: 16438

Use this

node.closest('td').find('input[type=hidden]').first().attr('value',item.id);

https://api.jquery.com/first/ as stated "Reduce the set of matched elements to the first in the set."

Your original code does this on the td and not input which is where the issue is. Also first() does not accept any parameters. You can use .find() to get the hidden input first and then use first() to reduce it to the first one

The solution can be applied to different forms

Upvotes: 1

void
void

Reputation: 36703

Use .find() to traverse the children

node.closest('td').find('input[name="carParams[2].id"]').attr('value',item.id);

Upvotes: 1

Related Questions