I’m working on a small web application that changes the contents of a select drop-down.
I was wondering if appendChild() and add() both accomplish the same task on a select DOM object in JavaScript?
var someSelect = document.getElementById('select-list');
var newOption = document.createElement('option');
someSelect.appendChild(newOption);
// The above is the same as the following?
someSelect.add(newOption);
In this case, I believe they would accomplish the same task.
add( )is provided specially via theHTMLSelectElementinterface specifically to add options to aselectelement and has a few extra options (such as an optional index at which to add the newoption, etc.).appendChild( )will give you a straight append of the child node.Watch out for different implementations of both methods cross-browser: I think that would be your biggest pain point. IIRC, the IEs would cause problems when appending children to
selects, so you might want toaddif that method exists and fall back onappendChild.