473,947 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="addPar agraph(0)">Item 1</li>
<li onclick="addPar agraph(1)">Item 2</li>
<li onclick="addPar agraph(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(in dex) {
var list = document.getEle mentById('list' );

var div = document.create Element('div');
div.className = 'lyrics';

var p = document.create Element('p');
var pTxt1 = document.create TextNode('Hello ');
p.appendChild(p Txt1);
div.appendChild (p);

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

May 20 '07 #1
4 19463
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="addPar agraph(0)">Item 1</li>
<li onclick="addPar agraph(1)">Item 2</li>
<li onclick="addPar agraph(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.javas cript 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.comwrot e:
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="addPar agraph(this)">I tem 1</li>
<li onclick="addPar agraph(this)">I tem 2</li>
<li onclick="addPar agraph(this)">I tem 3</li>
</ul>

function addParagraph(el e) {
if(ele.para){
var parS=ele.para.s tyle;
parS.display=(p arS.display=="n one")?'':'none' ;
return
}
var div = document.create Element('div');
div.className = 'lyrics';
var p = document.create Element('p');
var pTxt1 = document.create TextNode('Hello ');
p.appendChild(p Txt1);
div.appendChild (p);

ele.para=div;
ele.parentEleme nt.insertBefore (div,ele);
}

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

I have an unordered list similar to this one:

<ul id=list>
<li onclick="addPar agraph(0)">Item 1</li>
<li onclick="addPar agraph(1)">Item 2</li>
<li onclick="addPar agraph(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(in dex) {
var list = document.getEle mentById('list' );

var div = document.create Element('div');
div.className = 'lyrics';

var p = document.create Element('p');
var pTxt1 = document.create TextNode('Hello ');
p.appendChild(p Txt1);
div.appendChild (p);

list.insertBefo re(div, list.getElement sByTagName("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.getElement sByTagName("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.create Element('li');
var li = list.getElement sByTagName("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.getEle mentById('list' );
var li = ul.getElementsB yTagName('li')[index];
var p = document.create Element('p');
var pTxt1 = document.create TextNode('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.comwrot e:
Hello,
I have an unordered list similar to this one:
<ul id=list>
<li onclick="addPar agraph(0)">Item 1</li>
<li onclick="addPar agraph(1)">Item 2</li>
<li onclick="addPar agraph(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(in dex) {
var list = document.getEle mentById('list' );
var div = document.create Element('div');
div.className = 'lyrics';
var p = document.create Element('p');
var pTxt1 = document.create TextNode('Hello ');
p.appendChild(p Txt1);
div.appendChild (p);
list.insertBefo re(div, list.getElement sByTagName("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.getElement sByTagName("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.create Element('li');
var li = list.getElement sByTagName("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.getEle mentById('list' );
var li = ul.getElementsB yTagName('li')[index];
var p = document.create Element('p');
var pTxt1 = document.create TextNode('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
2333
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 last element of a list. (.end() returns an iterator one item past the end of the list) How could I solve this?
7
12673
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 added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
21
4028
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
3
1738
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 little different. Can anyone write couple to lines of code for this? Moreover, If I want to insert any element at the mth place of nth col in list box of type Report. can I achieve it. Thanks in anticipation.
3
3819
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 isn't working. It neither removes the item from the arraylist nor from the combobox like it's supposed to do. A couple of notes: cbx is the name of the combobox within my usercontrol, file is the name of the arraylist within my usercontrol. To try...
1
2314
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 element from a function to a name or calling the funtion twice. e = ET.Element('name') e.append(get_subelement(obj)) # but raises an exception on None
6
2812
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 strings I'm searching for. For each string, I need to search through the XML file, and when I find that string (in the tag <code>), copy the entire <item> XML element that the code appears in, into another text file. The XML document is comprised...
15
2768
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 pick from a list a random element that satisfies a given property. All or none of the elements may have the property. Most of the time, many of the elements will satisfy the property, and the property is a bit expensive to evaluate. Chance of...
11
1405
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', '5VT.Cat', (33060)) element1 = ('/smsc/chp/aztec/padlib/5VT.Cat2',
0
9982
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
11348
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10692
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9888
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7430
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6116
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4946
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4538
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.