/    Sign up×
Community /Pin to ProfileBookmark

Dynamically adding items to a dynamically created list

Hi All,

I have a form with a button. On clicking the button, i need to dynamically add a list to the form and then add items to that list. I was able to add a list dynamically. But i am not able to add items to that list. I have seen many examples where the list already exists in the form, and we can dynamically add items to that list(with help of ‘document.form.list_name.options.add(new Option(‘the item’)) . But in my case, the list itself is dynamically created. So i am confused on adding items dynamically.

I tried calling two functions in the button click. In the first function the list will be added and in the second function the items get added. But this does not work.

Here is my code:

[CODE]<html>
<head>
<script language = “javascript”>
function selectItems()
{
var select_type = document.createElement(‘select’);
var type = document.createAttribute(‘name’);
var id = document.createAttribute(‘id’); select_type.setAttribute(type,’related_items’); select_type.setAttribute(id,’related_items’);
document.body.insertBefore(select_type);
}

function dynamicItems()
{
document.ss.related_items.options.add(new Option(‘Chennai’); //related_items is the id of the list which i dynamically created
}

<body>
<form name=’ss’>
<input type=”button” value=”click” onClick=”selectItems(),dynamicItems()”> //called two functions
</form>
</body>
</html>[/CODE]

Can anyone please help me on this?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERDec 30.2008 — See post #5 of: http://www.webdeveloper.com/forum/showthread.php?t=197594

Should be able to do what you want if I understand the problem.
Copy linkTweet thisAlerts:
@vaishnaviauthorDec 31.2008 — JMRKER, I went through the post in the link. Even there, the list is already available and we are only adding items dynamically. But in my case, the list itself is created dynamically. So how to add items to the dynamically created list?

Can anyone please help me?
Copy linkTweet thisAlerts:
@SangeeKarthikDec 31.2008 — Hi vaishnavi

I think this code should help you out.

<html>

<head>

<script language = "javascript">

function selectItems()

{

var select_type = document.createElement('select');

select_type.id = 'related_items';

var oOption = document.createElement("option");

var t0 = document.createTextNode("Chennai");

oOption.setAttribute("value", 0);

oOption.appendChild(t0);

select_type.appendChild(oOption);

var oOption1 = document.createElement("option");

var t0 = document.createTextNode("Bangalore");

oOption1.setAttribute("value", 1);

oOption1.appendChild(t0);

select_type.appendChild(oOption1);

document.getElementById('selectDiv').appendChild(select_type);

}


</script>

<body>

<form name='ss'>

<div id='selectDiv' ></div>

<input type="button" value="click" onClick="selectItems()">

</form>

</body>

</html>

Just add a dummy div to which this select is going to be appended. and add options in the above said way.

Let me know if this helps u solve the issue

Sangeetha...
Copy linkTweet thisAlerts:
@JMRKERDec 31.2008 — Another alternative solution:
[code=php]
<html>
<head>
<title>Add to Select Box</title>
<script type="text/javascript">
var Item_Valu = [];
function Populate(IDS,Items){
var tmp = [];
var sel = document.getElementById(IDS);
sel.options.length=0;
sel.options[0]=new Option('[SELECT]','',true,true);
for (var zxc0=0;zxc0<Items.length;zxc0++){
if (Items[zxc0]){
tmp = Items[zxc0].split(':');
sel.options[sel.options.length]=new Option(tmp[1],tmp[0],true,true);
}
}
sel.selectedIndex=0;
}
function AddToList() {
var item = prompt('Enter item display','');
if ((item == '') || (item == null)) { return; }
var valu = prompt('Enter item value','');
if ((valu == '') || (valu == null)) { return; }
Item_Valu.push(valu+':'+item);
Populate('SBox',Item_Valu);
}
function ShowValu() { // for testing purposes only
var elem = document.getElementById('SBox');
var msg = 'No selections available';
if (elem.selectedIndex >= 0) {
msg = 'Current choice: '+elem[elem.selectedIndex].value+ ' for '+elem[elem.selectedIndex].text;
}
alert(msg);
}
</script>
</head>
<body>
<button onclick="AddToList()">Add to list</button>
<select type="text" id="SBox"></select>
<button onclick="ShowValu()">Current Value</button>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@vaishnaviauthorJan 01.2009 — Sangeetha,

That was great! Thank you very much. Your createTextNode(..) was good. It worked for me

I think JMRKER has a method which can be iterated over multiple no of times and also has some more functionality like getting the item from the user and inserting into the list. That also looks good. I am yet to try it.

Thanks JMRKER and Sangeetha:p
Copy linkTweet thisAlerts:
@JMRKERJan 01.2009 — From the both of us, you are most welcome.

Glad we were able to help

Good Luck!

?
×

Success!

Help @vaishnavi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...