2

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?

Rode
  • 51
  • 1
  • 8

3 Answers3

3

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();
}
MrCode
  • 59,851
  • 9
  • 76
  • 106
2

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);
Kaf
  • 30,825
  • 7
  • 51
  • 74
  • 2
    Thanks, good solution. Would vote you up if I could but I went with MrCodes mootool solution. – Rode Oct 14 '13 at 09:34
0
$("DIV#21, INPUT[value='21']").remove()
Adrian Ber
  • 17,162
  • 9
  • 57
  • 99