Priya
Priya

Reputation: 1554

how to remove a html tag from the parent in jquery

<td>
    <input type='checkbox' value='-1' name='xxx' checked>XXX
    <input type='hidden' value='-1' name='xxx'>
</td>
<td>
    <input type='checkbox' value='-1' name='xyy' checked>Xyy
    <input type='hidden' value='-1' name='xyy'>
</td>
<td>
    <input type='checkbox' value='-1' name='yyy' checked>yyy
    <input type='hidden' value='-1' name='yyy'>
</td>

In the above code I set a hidden value with same name as on submition of form neither 0 nor 1 is being submitted. Now I got a problem that when I click the checkbox I want to remove the input:hidden node and remain will same. But using $("input:hidden").remove() its removing the total hidden values present in the document.

Upvotes: 1

Views: 183

Answers (5)

Tiquelou
Tiquelou

Reputation: 459

Your solution is needlessly complicated if you ask me. An <input type='checkbox' value='-1' name='xxx' checked/> returns the value to the backend if it is checked else it returns nothing. So essentially it does the same thing you are trying to accomplish by putting those extra hidden fields and removing them - a lot more efficiently, I might add.

Please do review the code you have written. You may refer here for knowing how checkboxes are handled.

Upvotes: 0

user1160251
user1160251

Reputation: 39

try this:

$("input:checkbox").click(function() { 
$(this).next("input:hidden").remove(); 
}); 

Upvotes: 1

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

<td>
    <input type='checkbox' value='-1' name='xyy' class='xyz' checked>Xyy
    <input type='hidden' value='-1' name='xyy'  class='xyz'>
</td>.

$("input.xyy:hidden").remove();

Added a class and managed the requirenment.

Upvotes: 0

Chuck Norris
Chuck Norris

Reputation: 15190

You can try this if you have that html structure. It simply removes next hidden input of checked checkbox.

$("input:checkbox").click(function() {
  $(this).next("input:hidden").remove();
});

If you want to delete them only when checkbox is checked you can add this condition

if ($(this).is (':checked'))

Upvotes: 1

Bon Espresso
Bon Espresso

Reputation: 693

$("input[name=xxx]:hidden").remove();

Upvotes: 1

Related Questions