How to add name attribute only to input in specific table row



Hello, i have table that are generated in while loop.

<table id="one">
      <td><input class="allInputs" value="1"></td>
      <td><input class="allInputs" value="2"></td>
      <td><input class="allInputs" value="3"></td>

then i have jQuery that do moving <tr> between two tables that works.

$('body').on('click','#one tbody tr td .checkbox.1', function(){
     //my moving tr code here

What i m trying to do is add name attribute to input in this <tr> but instead it add it to all of them :frowning:
This is what i got:

$('body').on('click','#one tbody tr td .checkbox.1', function(){
    $(this).closest('tr').find("input").each(function() {
         $('.allInputs').attr('name', 'changeIdTwo[]');


Not with the table code as shown.

It's because of that: $('.allInputs') which is unaware of the context.


@Dormilich how can i make this work?

$(this).closest('tr').find('input.allInputs').prop('name', 'changeIdTwo[]');


@Dormilich this does not seems to be working :frowning:
also no error returned!


Create a fiddle, where we can have a look at the issue, since the posted code is insufficient for that.

It's not an error if the selector does not match anything.



Will make fiddle if you will think this is wrong:
i did like this and seems its working:

var row = $(this).closest('tr').clone();
$(row).find('input.allInputs').prop('name', 'changeIdTwo[]');

is it correct?


there is no syntax error that I see as far as JS and jQuery are concerned.


Ok thanks for help :slight_smile:


any idea why $(row).find('input.allInputs').removeProp('name', 'changeIdTwo[]');
does not work?
i also tried $(row).find('input.allInputs').removeProp('name');


because name is a native property.

In general, it is only necessary to remove custom properties that have been set on an object, and not built-in (native) properties.

meaning what exactly?


@Dormilich looks like removeAttr did a job :slight_smile:


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.