Dev
Dev

Reputation: 791

jQuery iterate through HTML in a variable and remove first radio button

I have a some HTML stored in a javascript variable that looks like below. Example looks like below.

var divHTML = "<table><tbody><tr><td><input type='radio' name='radio_1'></td></tr><tr><td></td><tr><td><input type='radio' name='radio_2'></td></tr></tbody></table>;

I would like to iterate through the divHTML and remove the first radio button along with the <tr></tr> that wrapped around it and return the rest of the HTML. (could acheive the same by removing the first <tr></tr> as well I think - as it will remove what's inside but the end goal is to not see the first radio button).

Also created a JSFiddle with the string in place. jsFiddle Please help. Hope the question is clear and many thanks in advance.

Upvotes: 0

Views: 336

Answers (2)

Blazemonger
Blazemonger

Reputation: 92923

You forgot a closing quote mark, by the way:

var divHTML = "<table><tbody><tr><td><input type='radio'></td></tr><tr><td></td><tr><td><input type='radio'></td></tr></tbody></table>";
var $divhtml = $(divHTML);
$divhtml.find(':radio:first').closest('tr').remove();

newHTML = $divhtml.clone().wrap('<div/>').parent().html();
alert(newHTML);

Upvotes: 3

El Ronnoco
El Ronnoco

Reputation: 11922

don't know what you're doing but will this work?

var divHTML = "<table><tbody><tr><td><input type='radio'></td></tr><tr><td></td><tr><td><input type='radio'></td></tr></tbody></table>"

$('body').append($(divHTML));

$("input:first").hide();

or you can hide the first radio button in your divHTML "markup" before appending to the <body>...

var newHTML = $(divHTML);

$("input:first",newHTML).hide();

$('body').append(newHTML);

JSFiddle here

Upvotes: 1

Related Questions