473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to add a new element after a list item?

SM
Hello,

I have an unordered list similar to this one:

<ul id=list>
<li onclick="addParagraph(0)">Item 1</li>
<li onclick="addParagraph(1)">Item 2</li>
<li onclick="addParagraph(2)">Item 3</li>
</ul>

When I click on a item, a paragraph is inserted after that item. That
part works fine. Check code below.
Im having a probem when i click on the same list item again. It keeps
adding the paragraph over and over again.
I don't know how to control it. I figure that i problably need some
sort of toggle function
Also, when i click on another list item, i want the previous list item
paragraph to go way! and the new one to appear

How can this be done with Javascript and DOM?

Need help.
Thanks
MArco

//function that adds a <pafter a list item
function addParagraph(index) {
var list = document.getElementById('list');

var div = document.createElement('div');
div.className = 'lyrics';

var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
p.appendChild(pTxt1);
div.appendChild(p);

list.insertBefore(div, list.getElementsByTagName("li")[index + 1]);
}
<style type="text/css">
.lyrics { width:300px; border:1px solid blue; }
</style>

May 20 '07 #1
4 19440
SM said the following on 5/20/2007 2:16 PM:
Hello,

I have an unordered list similar to this one:

<ul id=list>
<li onclick="addParagraph(0)">Item 1</li>
<li onclick="addParagraph(1)">Item 2</li>
<li onclick="addParagraph(2)">Item 3</li>
</ul>

When I click on a item, a paragraph is inserted after that item. That
part works fine. Check code below.
Im having a probem when i click on the same list item again. It keeps
adding the paragraph over and over again.
I don't know how to control it. I figure that i problably need some
sort of toggle function
Also, when i click on another list item, i want the previous list item
paragraph to go way! and the new one to appear

How can this be done with Javascript and DOM?
Place all the paragraphs in the page. Position them accordingly, then
toggle the .style.display property from block to none. Loop through the
paragraphs and toggle them. If it is block, set it to none and vice versa.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 20 '07 #2
On May 20, 12:16 pm, SM <servandomont...@gmail.comwrote:
Hello,

I have an unordered list
When I click on a item, a paragraph is inserted after that item. That
part works fine. Check code below.
Im having a probem when i click on the same list item again. It keeps
adding the paragraph over and over again.
I don't know how to control it. I figure that i problably need some
sort of toggle function
try this:
<ul id=list>
<li onclick="addParagraph(this)">Item 1</li>
<li onclick="addParagraph(this)">Item 2</li>
<li onclick="addParagraph(this)">Item 3</li>
</ul>

function addParagraph(ele) {
if(ele.para){
var parS=ele.para.style;
parS.display=(parS.display=="none")?'':'none';
return
}
var div = document.createElement('div');
div.className = 'lyrics';
var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
p.appendChild(pTxt1);
div.appendChild(p);

ele.para=div;
ele.parentElement.insertBefore(div,ele);
}

May 21 '07 #3
On May 21, 4:16 am, SM <servandomont...@gmail.comwrote:
Hello,

I have an unordered list similar to this one:

<ul id=list>
<li onclick="addParagraph(0)">Item 1</li>
<li onclick="addParagraph(1)">Item 2</li>
<li onclick="addParagraph(2)">Item 3</li>
</ul>

When I click on a item, a paragraph is inserted after that item. That
part works fine. Check code below.
Im having a probem when i click on the same list item again. It keeps
adding the paragraph over and over again.
I don't know how to control it. I figure that i problably need some
sort of toggle function
Also, when i click on another list item, i want the previous list item
paragraph to go way! and the new one to appear

How can this be done with Javascript and DOM?

Need help.
Thanks
MArco

//function that adds a <pafter a list item
function addParagraph(index) {
var list = document.getElementById('list');

var div = document.createElement('div');
div.className = 'lyrics';

var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
p.appendChild(pTxt1);
div.appendChild(p);

list.insertBefore(div, list.getElementsByTagName("li")[index + 1]);
Inserting a div into a ul element creates invalid HTML. No doubt
browsers are using error correction to do something with it, but to
get consistent results you must enter the div as a child of the li
element, not the ul.

In any case, the div doesn't seem to do anything other than wrap the p
element, so why not just insert that?

I also would not expect list.getElementsByTagName("li")[index + 1] to
work always as it create a reference to a non-existent element. If
that was required (say you are inserting an li after another one) you
should use nextSibling:

var newLi = document.createElement('li');
var li = list.getElementsByTagName("li")[index];
ul.insertBefore(newLi, li.nextSibling);
But that isn't needed in this case since you aren't inserting into the
UL, you should be inserting into the LI, so try something like:

var ul = document.getElementById('list');
var li = ul.getElementsByTagName('li')[index];
var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
li.appendChild.(p);

--
Rob

May 21 '07 #4
SM
On May 21, 2:28 am, RobG <r...@iinet.net.auwrote:
On May 21, 4:16 am, SM <servandomont...@gmail.comwrote:
Hello,
I have an unordered list similar to this one:
<ul id=list>
<li onclick="addParagraph(0)">Item 1</li>
<li onclick="addParagraph(1)">Item 2</li>
<li onclick="addParagraph(2)">Item 3</li>
</ul>
When I click on a item, a paragraph is inserted after that item. That
part works fine. Check code below.
Im having a probem when i click on the same list item again. It keeps
adding the paragraph over and over again.
I don't know how to control it. I figure that i problably need some
sort of toggle function
Also, when i click on another list item, i want the previous list item
paragraph to go way! and the new one to appear
How can this be done with Javascript and DOM?
Need help.
Thanks
MArco
//function that adds a <pafter a list item
function addParagraph(index) {
var list = document.getElementById('list');
var div = document.createElement('div');
div.className = 'lyrics';
var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
p.appendChild(pTxt1);
div.appendChild(p);
list.insertBefore(div, list.getElementsByTagName("li")[index + 1]);

Inserting a div into a ul element creates invalid HTML. No doubt
browsers are using error correction to do something with it, but to
get consistent results you must enter the div as a child of the li
element, not the ul.

In any case, the div doesn't seem to do anything other than wrap the p
element, so why not just insert that?

I also would not expect list.getElementsByTagName("li")[index + 1] to
work always as it create a reference to a non-existent element. If
that was required (say you are inserting an li after another one) you
should use nextSibling:

var newLi = document.createElement('li');
var li = list.getElementsByTagName("li")[index];
ul.insertBefore(newLi, li.nextSibling);

But that isn't needed in this case since you aren't inserting into the
UL, you should be inserting into the LI, so try something like:

var ul = document.getElementById('list');
var li = ul.getElementsByTagName('li')[index];
var p = document.createElement('p');
var pTxt1 = document.createTextNode('Hello');
li.appendChild.(p);

--
Rob
Thanks guys,

Marco

May 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: xerix | last post by:
I'm trying to make a function which insert an object into a list and returns an iterator to the new element in the list. But as far as I know there is no function which returns an iterator to the...
7
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
3
by: Vijay | last post by:
Hi, Iam new to VC++6.0. I want to display selected item of the ListBox in a MessageBox. I know for experience ppl it must be a kids play but for me who had worked in C# and VB, it seems to be...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
1
by: Ron Adam | last post by:
In my program I have a lot of statements that append elements, but sometimes I don't want to append the element so it requres an if statement to check it, and that requires assigning the returned...
6
by: mandibdc | last post by:
I need to extract some elements from a very large XML file. Because of the size, I'd like to work with it on my Linux machine as a text file. Basically, I am going to have a list of specific...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
11
by: rh0dium | last post by:
Hi all, I have a simple list to which I want to append another tuple if element 0 is not found anywhere in the list. element = ('/smsc/chp/aztec/padlib/5VT.Cat', '/smsc/chp/aztec/padlib',...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.