The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Adding dropdown list on a cell
Discuss Adding dropdown list on a cell in the JavaScript Development forum on Dev Shed. Adding dropdown list on a cell JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 18th, 2012, 12:56 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 20 m 36 sec
Reputation Power: 0
|
|
|
Adding dropdown list on a cell
I have a dropdown list on each cell of table row. Now what I need is dynamically add more dropdownlist on a particular cells with pressing the add button. Any code snippet to help on this? Thank you.
|

September 23rd, 2012, 01:10 AM
|
 |
Contributing User
|
|
|
|
Code:
<script>
function addList(cellID,ddlistVal,ddlistTxt)
{
// get option parameters
var optionVal = ddlistVal.split(",");
var optionText = ddlistTxt.split(",");
// define select list
var sel = document.createElement("select");
// add list
document.getElementById(cellID).appendChild(sel);
// selected option
var optSEL = document.createElement("option");
var optsel = document.createTextNode(optionText[0]);
optSEL.setAttribute("value","");
for (i=0;i<=optionText.length-1;i++) {
// add options
var opts = document.createElement("option");
var optsVal = document.createTextNode(optionText[i]);
opts.setAttribute("value",optionVal[i-1]);
// add options
if (i != 0) {
document.getElementById(cellID).getElementsByTagName("select")[0].appendChild(opts);
document.getElementById(cellID).getElementsByTagName("option")[i].appendChild(optsVal);
}
else {
document.getElementById(cellID).getElementsByTagName("select")[0].appendChild(optSEL);
document.getElementById(cellID).getElementsByTagName("option")[0].appendChild(optsel);
}
}
// select initial option
document.getElementById(cellID).getElementsByTagName("option")[0].setAttribute("selected","selected");
}
</script>
<table>
<tr>
<td id="td1">
<!-- add another dropdown list to this cell -->
</td>
</table>
<input type="button" value="Add" onclick="javascript:addList('td1','Option A,Option B,Option C','Select An Option,Option 1,Option 2,Option 3')"/>
|

September 24th, 2012, 11:04 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 3
Time spent in forums: 20 m 36 sec
Reputation Power: 0
|
|
Dear Loone,
Thank you and will try out your solution even thought I managed some other solution too.
Quote: | Originally Posted by web_loone08
Code:
<script>
function addList(cellID,ddlistVal,ddlistTxt)
{
// get option parameters
var optionVal = ddlistVal.split(",");
var optionText = ddlistTxt.split(",");
// define select list
var sel = document.createElement("select");
// add list
document.getElementById(cellID).appendChild(sel);
// selected option
var optSEL = document.createElement("option");
var optsel = document.createTextNode(optionText[0]);
optSEL.setAttribute("value","");
for (i=0;i<=optionText.length-1;i++) {
// add options
var opts = document.createElement("option");
var optsVal = document.createTextNode(optionText[i]);
opts.setAttribute("value",optionVal[i-1]);
// add options
if (i != 0) {
document.getElementById(cellID).getElementsByTagName("select")[0].appendChild(opts);
document.getElementById(cellID).getElementsByTagName("option")[i].appendChild(optsVal);
}
else {
document.getElementById(cellID).getElementsByTagName("select")[0].appendChild(optSEL);
document.getElementById(cellID).getElementsByTagName("option")[0].appendChild(optsel);
}
}
// select initial option
document.getElementById(cellID).getElementsByTagName("option")[0].setAttribute("selected","selected");
}
</script>
<table>
<tr>
<td id="td1">
<!-- add another dropdown list to this cell -->
</td>
</table>
<input type="button" value="Add" onclick="javascript:addList('td1','Option A,Option B,Option C','Select An Option,Option 1,Option 2,Option 3')"/>
|
|

September 24th, 2012, 11:22 AM
|
 |
Contributing User
|
|
|
|
|
Cool, glad you got something to work out for you.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|