Solved

how do I toggle bolding text?

Posted on 2009-05-12
4
545 Views
Last Modified: 2012-05-06
Hello Experts,

Please review code below.
I would like to bold the div text when clicked and want to unbold the previous one clicked (toggle).

See what I mean?

Thx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 

<head>

<title>JS toggle</title>

<script language="JavaScript">
 

function bold(id){

var current = "";

	if(id == "one"){

		var bold = document.getElementById(id);

		bold.style.fontWeight = "bold";

		current = id;

		

		if(current){

			var unbold = document.getElementById(current);

			unbold.style.fontWeight = "none";

		}

	}

	if(id == "two"){

		var bold = document.getElementById(id);

		bold.style.fontWeight = "bold";

		current = id;

		

		if(current){

			var unbold = document.getElementById(current);

			unbold.style.fontWeight = "none";

		}

	}

	if(id == "three"){

		var bold = document.getElementById(id);

		bold.style.fontWeight = "bold";

		current = id;

		

		if(current){

			var unbold = document.getElementById(current);

			unbold.style.fontWeight = "none";

		}

	}

}
 

</script>

</head>
 

<body>

<div id="one" onclick="bold('one'); return false; "> text </div>

<div id="two" onclick="bold('two'); return false; "> text </div>

<div id="three" onclick="bold('three'); return false; "> text </div>

</body>

Open in new window

0
Comment
Question by:epifanio67
  • 2
4 Comments
 
LVL 15

Assisted Solution

by:ncoo
ncoo earned 100 total points
ID: 24366549
You could do the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 

<head>

<title>JS toggle</title>

<script language="JavaScript">
 

function bold(id){

	var elem = document.getElementById(id);

	if (elem.style.fontWeight=='bold') {

		elem.style.fontWeight ='';

	} else {

		elem.style.fontWeight ='bold';

	}

}

</script>

</head>
 

<body>

<div id="one" onclick="bold('one'); return false; "> text </div>

<div id="two" onclick="bold('two'); return false; "> text </div>

<div id="three" onclick="bold('three'); return false; "> text </div>

</body>

Open in new window

0
 

Author Comment

by:epifanio67
ID: 24366612
Thx ncoo,

I just tested your suggestion.
When I click on div one, the text does bold. However, when I click on div two, it also bold; but it does not unbold div one.

They all bold when clicked. I want to be to have one bold text/div per click.

See what I mean?
Thx,
0
 

Accepted Solution

by:
kostyak earned 400 total points
ID: 24366928
epifanio67:
Here's the solution that I would go with:

Hope I did not oversimplify your code, and you get what you're looking for...
<html>

<head>

<title>JS toggle</title>

<script language="JavaScript">

var current = "";
 

function bold(obj){

	obj.style.fontWeight = 'bold';

	if(current != '') {

	    document.getElementById(current).style.fontWeight = '';

	}

	current = obj.id;

} 

</script>

</head>

 

<body>

<div id="one" onclick="bold(this); return false; "> text </div>

<div id="two" onclick="bold(this); return false; "> text </div>

<div id="three" onclick="bold(this); return false; "> text </div>

</body>

Open in new window

0
 

Author Closing Comment

by:epifanio67
ID: 31580613
Excellent Kostyak... it is exactly what I was looking for....

Regards,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Sheets Script 21 58
Download a website to hdd 2 52
Rhino javascript replace special characters 4 21
geting geocode of a user with using Google maps api 8 19
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now