Jump to content
  • Advertisement
Sign in to follow this  
plywood

[web] DHTML ULs?

This topic is 3072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've come across a situation where I want my users to be able to make a dynamic list of things, and then commit the entire list to the database after they have finished making it.

What I'd like to have is a simple text input field that had a button to the right of it that simply reads "Add". When the user enters input into the text field and clicks "Add", an unordered list somewhere else on the page loads a new bullet with the newly-added text input.

The only problem is... I can't seem to figure out how to dynamically change lists. And, even more scary than that, from what I can collect, the HTML DOM doesn't even support this function!

Here's my code:

<script type="text/javascript">
function AddToList()
{
var newItem = document.getElementById("newItem").value;

// if newItem = "Widget", then I need to add
// <li>Widget</li> to the-list... but how?!?!
}
</script>

<input type="text" id="newItem" size="30"/>&nbsp;
<input type="button" value="Add" onClick="AddToList();"/>

<p>

<ul id="the-list">
</ul>



Thanks for any input

Share this post


Link to post
Share on other sites
Advertisement
I'd recommend taking a look at a JavaScript framework (such as my personal favourite, MooTools) as these tend to make DOM manipulation nice and easy.

<script type="text/javascript">
var AddToList = function() {
// Create a new list element (li).
var newItem = new Element('li');
// Set the new item's text to the value of the newItem field - $() is the MooTools shortcut for document.getElementById()
newItem.set('text', $('newItem').get('value'));
// Inject the new item into the bottom of the element 'the-list'.
newItem.inject('the-list', 'bottom');
}
var AddToListShorter = function() {
// This does the same as the above but is shortened quite considerably!
new Element('li', {
text : $('newItem').get('value')
}).inject('the-list');
}
</script>

Share this post


Link to post
Share on other sites
benryves,

I took your advice and tried MooTools - thanks! They work like a charm. So no that I have my list dynamically populating (per your script example), I'm trying to figure out how to iterate my list in JS/MooTools, and store the list items in a javascript array. Here's the code skeleton:


function AddToList()
{
// ... everything benryves posted above
}

function SaveAsArray()
{
// 1. Declare a variable that represents all the children of UL tag.
var children = $('the-list').getChildren();

// 2. Iterate the array, and save the content of the invidiaul <li> tags
// in each index of a new array
var listItems = new Array();
for(i = 0; i < children.length; i++)
{
listItems = // ????
}

// 3. Do something with the listItems array
}



I'm stuck at step 2. MooTools is awesome but has a learning curve associated with its API. Any nudge in the right direction is enormously appreciated!

thanks all,
ply

Share this post


Link to post
Share on other sites
Arrays are extended by MooTools to have an .each() function associated with them. The special function $$ supports CSS selectors. Combining these two allows you to write something like this:

var SaveAsArray = function() {

// Create an empty array to store the text of the items.
var listItems = [];

// Use $$ to select the li elements inside #the-list and .each to iterate over the elements.
$$('#the-list li').each(function(li) {
// Append the items to the array.
listItems.push(li.get('text'));
});

// Do something with the list of items.
alert(listItems.join(', '));

}

($('the-list').getChildren() and $$('#the-list li') do exactly the same thing in this instance, so there's no particular reason I used $$ instead, but thought it worth pointing out). [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!