Question

guys, once again I come begging for help. I can't get my head around this - all I need is update the name attribute's indexes of texts in my form. I want to show to the user one line in the table and, as he/she clicks on the plus sign, another line is added. For my form to work on the server side, I need do reindex the name parameter. That's how I'm trying to do it:

function reindex () {
$("table").find("tr.faixa").each(function(index, value) {
value.find("input.faixa_min").attr("name", "data[" + index + "][BonusRecarga][faixa_min]"); // <- here's where I get the error
value.find("input.faixa_max").attr("name", "data[" + index + "][BonusRecarga][faixa_max]");
value.find("input.valor_bonus").attr("name", "data[" + index + "][BonusRecarga][valor_bonus]");
value.find("input.perc_bonus").attr("name", "data[" + index + "][BonusRecarga][perc_bonus]");
});      
}

And here's a fragment of the form:

<tr class="faixa">
<td>
<input name="data[0][BonusRecarga][faixa_min]" class="faixa_min" type="text" required="required"/>
 </td>                  
<td>
<input name="data[0][BonusRecarga][faixa_max]" class="faixa_max" type="text" required="required"/>
</td>
<td>
<input name="data[0][BonusRecarga][valor_bonus]" class="valor_bonus" type="text" required="required"/>
</td>
<td>
<input name="data[0][BonusRecarga][perc_bonus]" class="perc_bonus input-small" type="text" required="required"/>
</td>
<td>
<span class="help-inline"><i class="icon-plus" style="cursor: pointer"></i></span>
<span class="help-inline"><i class="icon-minus" style="cursor: pointer"></i></span>
</td>
</tr>

I have also tried using a common for(var i = 0; i < $(".faixa").lenght; i++), but I always get the "undefined is not a function" in the first value.find(...). It looks like "value" doesn't have the find() method, but when I log it, it shows me a regular "....

Do you know why the find() method is not working on the "value" variable?

Thanks in advance.

Was it helpful?

Solution

value is a DOM Element object while .find() is a jQuery method. You need to create a jquery object :

var $value = $(value);
//Then use $value.find()

OTHER TIPS

Try using $(this).find('input...')... . it works, and is better inside each.

A much faster result for jquery would be to use the EQ function and append. https://api.jquery.com/eq/

if EQ is not what your looking for leave a comment and ill look further into your issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top