-1

How I can duplicate the following content in JavaScript? I want to have two dropdown's in the same page.

<fieldset id="product-options-wrapper" class="product-options">
  <dl class="last">
    <dt>
    <label class="required">
    <em>*</em>
    Pack Size/Price
    </label>
    </dt>
    <dd class="last">
      <div class="input-box">
        <select id="attribute148" class="required-entry super-attribute-select" name="super_attribute[148]" onchange="return changeSku(148, this);">
          <option value="">Choose an Option...</option>
          <option value="76">10g: €9.60</option>
          <option value="61">25g: €17.40</option>
          <option value="78">100g: €43.80</option>
        </select>
      </div>
    </dd>
  </dl>

Thank you

Nope
  • 22,147
  • 7
  • 47
  • 72
Robert
  • 812
  • 3
  • 18
  • 47
  • 2
    use https://api.jquery.com/clone/ – Rahul Jan 11 '17 at 11:11
  • why would you want to have 2 select with same options in one page?use `.find()` with `.clone()` – guradio Jan 11 '17 at 11:12
  • Also, remove the id and use only class or set a different id for the cloned element. – The Alpha Jan 11 '17 at 11:12
  • 1
    Note: the ` – Peter B Jan 11 '17 at 11:12
  • Please see ► [**how-to-copy-a-dom-node-with-event-listeners**](http://stackoverflow.com/questions/15408394/how-to-copy-a-dom-node-with-event-listeners) and if using jQuery ► [**jQuery Clone**](https://api.jquery.com/clone/) Try those and if you have issues, feel free to post your code and can help you with it. – Nope Jan 11 '17 at 11:20

3 Answers3

1
var $select = document.querySelector('select#attribute148');
$select.id = "newId";
document.querySelector('yourNewDivId').appendChild($select);

First you get the actual select to JS. Then you update the id and at last you add it to the div, where you wanna have it.

EDIT for Comment

You could build a template:

function createSelect(id, options){
    var $select = document.createElement('select');
    $select.id = id;
    for(var i = 0; i < options.length; i++){
        var $option = document.createElement('option');
        $option.value = options[i].value;
        $option.innerHTML = options[i].name;
        $select.appendChild($option);
    }
    return $select;
}
var select = createSelect(48, [{value: 76, name: "10g: €9.60}, ...]);
document.querySelector('target').appendChild(select);
DomeTune
  • 1,401
  • 10
  • 21
0

You can use the cloneNode method for this.

var el = document.getElementById('product-options-wrapper');
var dupEl = el.cloneNode();

dupEl.id = "some-new-id";
document.getElementById('some-container').appendChild(dupEl);

EDIT: Updated with full code example

Stuart
  • 201
  • 1
  • 11
0

Test this out!

document.getElementById("product-options-wrapper").onclick = function(){ // when the fieldset is clicked
    var clone = this.cloneNode(true); 
    this.appendChild(clone); // clone it and append to the last fieldset
}

When a user clicks on the specified fieldset, it will clone it, and append the new clone, to the previous.

You can change what the new clone is appended to by changing this.appendChild(clone) to document.getElementById("yourid").appendChild(clone)

Hope this helps! :-)

GROVER.
  • 4,071
  • 2
  • 19
  • 66
  • Thank you but what I need is to duplicate that dropdown, in fact to duplicate as a list, I want to get the dropdown options and then to clone this and list in another div. so I don't need any click event – Robert Jan 11 '17 at 11:31