Bruce Walker
Bruce Walker

Reputation: 85

How to remove an element attribute from a clone element using jquery?

I'm able to clone a row using the following jquery code below successfully

$selectedRow.closest("tr").clone(true);

Within this clone tr, I need to remove the rowspan attribute from this td...

<td class="column1" rowspan="1">Testing this column</td>

I tried the following code

$selectedRow.closest("tr").clone(true).find(".column1").removeAttr("rowspan");

The problem with the above code is that it over shows this but it does remove the rowspan attribute:

<td class="column1">Testing this column</td>

I need it to show everything as in the previous code I wrote above:

$selectedRow.closest("tr").clone(true);

Upvotes: 0

Views: 2582

Answers (2)

Ram
Ram

Reputation: 144689

You can use the end method which ends the current filtering operation and returns the previous set which is the cloned tr.

var $clonedRow = $selectedRow.closest("tr")
                             .clone(true)
                              // find the `.column1` descendant of the cloned row
                             .find(".column1") 
                             .removeAttr("rowspan")
                             // end the current filtering operation 
                             // and return the cloned `tr`
                             .end(); 

Note that if the $selectedRow refers to a tr element closest doesn't do anything in this case, as it starts from testing the element itself and then traverses up through its ancestors in the DOM.

Upvotes: 2

Evil Buck
Evil Buck

Reputation: 1068

var $newClone = $selectedRow.closest("tr").clone(true);
$newClone.find(".column1").removeAttr("rowspan");

Upvotes: 0

Related Questions