Solved

how do I toggle bolding text?

Posted on 2009-05-12
4
533 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent Kostyak... it is exactly what I was looking for....

Regards,
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now