473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing text dynamically?

1 New Member
Is there a way to use javascript to change/update a class depending upon a click?

For example:
INITIAL HTML

[HTML]<h1 class="superhead">TODDLER</h1>
<ul class="extended">
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (6 - 7)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (8 - 10)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (11 - 12)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>[/HTML]

Clicking on KIDS (6 -7) will produce:

[HTML]<h1 class="superhead">TODDLER</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (6 - 7)</h1> <!-- CLICK ON H1 ELEMENT -->
<ul class="extended"> <!-- AND THE UL CLASS CHANGES FOR BOTH THIS ONE AND THE TODDLER ONE -->
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (8 - 10)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (11 - 12)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>[/HTML]

Then, you click on KIDS (11 - 12) and you get this:

[HTML]<h1 class="superhead">TODDLER</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (6 - 7)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (8 - 10)</h1>
<ul>
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>

<h1 class="superhead">KIDS (11 - 12)</h1>
<ul class="extended">
<li>Sneakers</li>
<li>Dress shoes</li>
<li>Tap / Ballet </li>
<li>Atheletic</li>
</ul>[/HTML]

...... and so on.
Nov 21 '07 #1
1 2390
phvfl
173 Recognized Expert New Member
Hi,

Welcome to TSDN,

You can change the class of an object by changing the className property of an object, for example if you had a variable called "$obj" you could change its class to "class1" as such:

Expand|Select|Wrap|Line Numbers
  1. $obj.className = 'class1';
  2.  
There are two main ways of selecting elements through DOM getElementById and getElementsByTagName, if there is an element that you know you will want to access through Javascript giving it an ID will let you select it directly:
Expand|Select|Wrap|Line Numbers
  1. <p id="p1">This is a paragraph</p>
  2.  
If there was a paragraph in your document as above you could select it with:
Expand|Select|Wrap|Line Numbers
  1. var $obj = document.getElementById("p1");
  2.  
Using the code above to change the class name the HTML would be changed to:
Expand|Select|Wrap|Line Numbers
  1. <p id="p1" class="class1">This is a paragraph</p>
  2.  
You could select all paragraphs in a document using the other method mentioned:
Expand|Select|Wrap|Line Numbers
  1. var $obj = document.getElementsByTagName("p");
  2.  
In this case an array of the paragraph elements would be returned.

This covers how you could change the class, you are wanting to have this happen when a heading is clicked. There are a few ways to achieve this, I will mention two.

Firstly you could hard code the action using the onclick tag of the HTML:
Expand|Select|Wrap|Line Numbers
  1. <p id="p1" onclick="alert('hello');">This is will say hello when clicked.</p>
  2.  
The text in the onclick attribute is the javascript to be executed. For anything of any substance it is tidier to use a function defined in the header or from an external file.
Expand|Select|Wrap|Line Numbers
  1. function sayHello(){
  2.   alert("hello");
  3. }
  4.  
Expand|Select|Wrap|Line Numbers
  1. <p id="p1" onclick="sayHello();">This is will say hello when clicked.</p>
  2.  
The second, cleaner way of adding the event to the element is to use the onclick property. Select the required element as previously and then set the onclick property to the function:
Expand|Select|Wrap|Line Numbers
  1. function sayHello(){
  2.   alert("hello");
  3. }
  4.  
  5. var $obj = document.getElementById("p1");
  6. $obj.onclick = function(){
  7. sayHello();
  8. };
  9.  
The only thing that would then be required would be to make sure that the javascript that set the onclick property executed once the document had loaded, otherwise the element being selected may not exist when the script executes. The best way to do this is to but the wiring of the events into a function that is called once the document is loaded, using the window.onload property:
Expand|Select|Wrap|Line Numbers
  1. function sayHello(){
  2.   alert("hello");
  3. }
  4.  
  5. function wireup(){
  6. var $obj = document.getElementById("p1");
  7. $obj.onclick = function(){sayHello();};
  8. }
  9.  
  10. window.onload = function(){wireup();}
  11.  
This may seem like a lot of work but it can save you a lot of code, for example if you wanted to execute the sayHello function when any paragraph was clicked you could change the wireup function slightly to:
Expand|Select|Wrap|Line Numbers
  1. function wireup(){
  2. var $obj = document.getElementsByTagName("p");
  3.   for(var i=0; i<$obj.length;i++){
  4.     $obj[i].onclick = function(){sayHello();};
  5.   }
  6. }
  7.  
If you had multiple elements that you wanted to be wired up to the same function you could do this with much less code this way.

I appreciate that I have not been specific to your example, but I have given a brief overview of all of the properties and methods that you would need to do what you want. Please have a go, if you have any questions about what I have written please let me know and I will help. If you have a try and have problems please post your attempted code .
Nov 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Free-Ed, Ltd. | last post by:
I am going nuts trying to find a paragraph in a book that described how to change the text content (HTML) in a DIV. Actually I have an array of HTML strings that I want to drop into the DIV,...
3
by: Billy | last post by:
Hello, I'm having a problem dynamically changing the color of a table background. I'm not sure exactly how to word this, but I'll give an example. I have a function called greentored(propname)...
31
by: Arthur Shapiro | last post by:
I'm the webmaster for a recreational organization. As part of one page of the site, I have an HTML "Calendar at a Glance" of the organization's events for the month. It's a simple table of a...
5
by: cjl | last post by:
Hey all: This code: if (stealth) { document.searchme.query.type = 'password'; } else {
0
by: Scott P. | last post by:
I'm creating an app using ASP .NET (my second app so bear with me here) that basically builds a PDF file based on a bunch of user selections. I have a page which displays a series of checkboxs...
4
by: Chris Mahoney | last post by:
Hi Currently I am setting the background image of my page by using the following code: <style type="text/css"> BODY { BACKGROUND-IMAGE: url(myimage.jpg) } </style> What I would like to do...
4
by: Alexander Mueller | last post by:
Hi all, in a CheckedListBox there are afaics two members that change the checked-flag for an item i.e. SetItemChecked and SetItemCheckState. In the online-help there is a remark that...
2
by: John | last post by:
Hi Everyone, I have a question about dynamically changing the length of a varchar(n) field, in case the value I'm trying to insert is too big and will give a "truncated" error, but before the...
1
by: hellohi | last post by:
Hello Everyone, I wanted to change the existing variable names dynamically.( like www.aol.com ) in this site images and some text is changing dynamically. like wise i need javascrip to write ...
4
by: bjjnova | last post by:
I need help dynamically resetting an event handler. I have a form with several buttons. I have intialized the Click event of a particular button thus on the form: _button_3.Click += new...
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
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...
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...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
muto222
php
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.