简体   繁体   中英

Changing radio buttons name using Javascript

I'm using a simple JS duplicate function to duplicate a div. Inside is form information with radio buttons, including one group called 'getOrRequest'. Each div represents a book and needs to have its own 'getOrRequest' value.

The name needs to be changed in order to make each duplicated group of radio buttons selectable without affecting every other radio button. What is the best way to change these values?

Here is how I'm duplicating the div, in case that is the issue.

var bookInfo = document.getElementById('bookInformation');
var copyDiv = document.getElementById('addListing').cloneNode(true);
bookInfo.appendChild(copyDiv);

I then have tried a couple methods of changing the name value. Like this:

bookInfo.copyDiv.getOrRequest_0.setAttribute("name", "'getOrRequest' + idNumber + '[]'");
bookInfo.copyDiv.getOrRequest_1.setAttribute("name", "'getOrRequest' + idNumber + '[]'");

As well as this:

bookInfo.copyDiv.getOrRequest_0.name = 'getOrRequest' + idNumber + '[]';
bookInfo.copyDiv.getOrRequest_1.name = 'getOrRequest' + idNumber + '[]';

getOrRequest_0 and getOrRequest_1 are the ID's of the input values, but I've tried it a few ways now and nothing seems to work. Thanks in advance!

EDIT: MORE INFO

Here is the specific code I'm using:

function addAnotherPost(){

var bookInfo = document.getElementById('bookInformation');
var copyDiv = document.getElementById('addListing').cloneNode(true);
var size = copyDiv.childNodes.length;

copyDiv.id = 'addListing' + idNumber;

for(var j = 0; j < size; j++){
   if(copyDiv.childNodes[j].name === "getOrRequest[]"){
      copyDiv.childNodes[j].name = "getOrRequest" + idNumber + "[]";
   }
}

bookInfo.appendChild(copyDiv);

idNumber++;
}

And it just doesn't seem to work.. The divs are duplicating, but the name value is not changing.

The trouble is you are looking for child nodes of the div, but the check boxes are not child nodes, they are descendant nodes. The nodes you are looking for are nested within a label. Update your code to look for all descendant inputs using copyDiv.getElementsByTagName("input") :

var idNumber = 0;
function addAnotherPost() {
    var bookInfo = document.getElementById('bookInformation');
    var copyDiv = document.getElementById('addListing').cloneNode(true);
    copyDiv.id = 'addListing' + idNumber;

    var inputs = copyDiv.getElementsByTagName("input");
    for(var j = 0; j < inputs.length; j++){
       if(inputs[j].name === "getOrRequest[]"){
          inputs[j].name = "getOrRequest" + idNumber + "[]";
       }
    }

    bookInfo.appendChild(copyDiv);
    idNumber++;
}

http://jsfiddle.net/gilly3/U5nsa/

You can try this - http://jsfiddle.net/ZKHF3/

    <div id="bookInformation">
        <div id="addListing">
            <input type="radio" name="addListing0[]" />
            <input type="radio" name="addListing0[]" />
        </div>
    </div>
    <button id="button">Add Listing</button>

    <script>
    document.getElementById("button").addEventListener("click", AddListing, false);

    var i = 1;
    var bookInfo = document.getElementById('bookInformation');

    function AddListing() {
        var copyDiv = document.getElementById('addListing').cloneNode(true);
        var size    = copyDiv.childNodes.length;

        copyDiv.id = "listing" + i;
        for ( var j = 0; j < size; j++ ) {
            if ( copyDiv.childNodes[j].nodeName.toLowerCase() == 'input' ) {
                copyDiv.childNodes[j].name = "addListing" + i + "[]";
            }
        }

        bookInfo.appendChild(copyDiv);
        i++;
    }
    </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM