JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 18th, 2012, 12:56 PM
frwa frwa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 frwa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old September 23rd, 2012, 01:10 AM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 658 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 2 m 36 sec
Reputation Power: 69
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')"/>

Reply With Quote
  #3  
Old September 24th, 2012, 11:04 AM
frwa frwa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 frwa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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')"/>

Reply With Quote
  #4  
Old September 24th, 2012, 11:22 AM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 658 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 2 m 36 sec
Reputation Power: 69
Cool, glad you got something to work out for you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Adding dropdown list on a cell

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap