How To Swap Two DropDown Select Using JavaScript
In This Post , We Will Learn How To Swap Values Of Two Dropdown Made Using <select> & <option> Tag
Swap Dropdown Using JavaScript
Lets Create Two Dropdown Using <select> Tag. Lets Provide Their id as "first" & "second" Respectively. Now Add Some Options Into Them Using <option> Tag.
After Adding Two Dropdown , Add One Button Using <button> Tag & Add One OnClick Event Into It Which Will Call Swap Function.
Now Create A JavaScript Function Using Which We Will Swap The Values Of Two Dropdown List. We Will Use Third Variable To Swap The Two Values Of Dropdown.
Code To Swap Two Dropdown List Using JavaScript
<script>
function swapit()
{
var second=document.getElementById("second");
var first=document.getElementById("first");
var temp;
temp=second.value;
second.value=first.value;
first.value=temp;
}
</script>
<!-- Code From NarendraDwivedi.Org -->
<select id="first">
<option value="USD" selected>USD</option>
<option value="INR">INR</option>
</select>
<select id="second">
<option value="USD">USD</option>
<option value="INR" selected>INR</option>
</select>
<button onClick="swapit()">Swap</button>
In This Way , We Have Successfully Swapped The Values Of Two Dropdown List Using JavaScript
Post a Comment