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

jquery

#1

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

<table id="one">
  <thead>
    <th>A</th>
    <th>B</th>
    <th>C</th>
  </thead>
  <tbody>
    <tr>
      <td><input class="allInputs" value="1"></td>
    </tr>
    <tr>
      <td><input class="allInputs" value="2"></td>
    </tr>
    <tr>
      <td><input class="allInputs" value="3"></td>
    </tr>
  </tbody>
</table>

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[]');
    });	
});

#2

Not with the table code as shown.

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


#3

@Dormilich how can i make this work?


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

#5

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


#6

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.


#7

@Dormilich

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?


#8

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


#9

Ok thanks for help :slight_smile:


#10

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


#11

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?


#12

@Dormilich looks like removeAttr did a job :slight_smile:


#13

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