views:

20

answers:

2

I have a div that is being duplicated dynamically. After duping it, I need to remove the name from the hidden input only, but can't seem to be able to select it only. The majority of the file is prototype, but I have jQuery at my disposal (jq).

I have managed to get the name to disappear, but it clears them all out, I only want it cleared on the duplicated div.

Help appreciated.

function dupField(fieldName,container,dupediv){
    var container = $(container);
    var dupedDiv = $(dupediv).cloneNode(true);
    var totalins=jq("#MI_name > div").size();
    newNode=document.createElement('div');
    newNode.innerHTML=dupedDiv.innerHTML;
    newNode.setAttribute('id',dupediv+totalins);
    newNode.setAttribute('class','MI_inz');
    var inputs_txt = newNode.getElementsByTagName('input');
    var x = jq('#MI_label').val();
    for(j=0;j<inputs_txt.length;j++){//change text field names
            var nameNow = inputs_txt[j].name;
            var newName = nameNow+totalins;
            inputs_txt[j].name=newName;
            inputs_txt[j].value='';
    }
    container.appendChild(newNode);
    var nn=jq('newNode:hidden');//jQuery - this selects all the hidden's in the form-no good-only want to change hidden input in newNode
    var hid=jq(nn).attr('name','');
}

It's the part after appendChild.

some of the other things I've tried:

var nn=jq('newNode:input(:hidden)');

-

var nn=jq(newNode).attr('id');
jq(nn+':hidden').attr('name','');

etc...

I'm sure it's simple, but I can't get it...

A: 
container.appendChild(newNode);
var nn=jq('newNode:hidden');
var hid=jq(nn).attr('name','');

You don't need to look up your new node. You already have a reference to it. Just use it to remove the name.

jq(newNode).attr('name', '');
Gregg
i don't want to rename the node itself, but rather a hidden input within it.
stormdrain
Right, my bad. Just caught that myself. sunn0 is the right answer, as indicated. But that's what I meant, use the existing node to find your element.
Gregg
+2  A: 

For an input where attribute type equals hidden:

jq(newNode).find('input[type=hidden]');
sunn0