Reputation: 51
I have a page with some values that looks like this:
<div id="productList" >
<input type="hidden" name="product[0].id" value="21">
<div id="21" >BeanCounter</div>
<input type="hidden" name="product[1].id" value="22">
<div id="22" >CallGate</div>
</div>
Now I want to delete the hidden field with value=21
and the div with id=21
.
I have this:
function remove(id){
var remove = $(id);
$('productList').removeChild(remove);
But how do I delete the hidden field? Can you get an object by its value?
edit missed the mootools tag, not sure where the jquery tag came from. I guess mootools isn't that common but hopefully this could be done with plain javascript.
Hmm tag got changed back to jquery again?
Upvotes: 2
Views: 3897
Reputation: 33839
Using plain javascript
as mentioned in the edited post:
var checkVal = '21';
var parentDiv = document.getElementById('productList');
var inps = parentDiv.getElementsByTagName('input');
var inpRemove = null;
//Get the div to be removed
var divRemove = document.getElementById(checkVal);
for(var i=0; i<inps.length; i++)
{
if(inps[i].value == checkVal)
{
//Get the input to be removed (Assuming only input with same value)
inpRemove = inps[i];
break;
}
}
//Remove both here
parentDiv.removeChild(inpRemove);
parentDiv.removeChild(divRemove);
Upvotes: 2
Reputation: 64536
Mootools solution:
function remove(id){
$('productList').getElements('input[value="' + id + '"], div[id="' + id + '"]').destroy();
}
jQuery solution:
function remove(id){
$('input[value="' + id + '"], div[id="' + id + '"]').remove();
}
Upvotes: 3