Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Javascript + CSS Problem.
Old 06-08-2007, 10:51 PM Javascript + CSS Problem. (If function, changing css).
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
I'm not a big fan of JavaScript, But I have written this code to open and shut a box. Below is the Code I'm using.

Code:
function description(element){
	if (element.className='description_open'){
		element.className='description';
	} 
	if (element.className='description') {
		element.className='description_open';
	} else {
		element.className='description';
	}
}
HTML Code:
<div class="description" onclick="description(this);">
The box opens ok, but will not close. Is something wrong with my Javascript?
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 06-08-2007 at 11:09 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
 
Register now for full access!
Old 06-09-2007, 01:28 AM Re: Javascript + CSS Problem.
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
I used to make this same mistake all the time and still do. In your comparison you're only using one = when you need two. The single = is the assignment.

element.className='description_open' is actually setting element.className to be equal to 'description_open' and not testing to see if they're the same.

What you want is

element.className=='description_open'

and the same for the other if statement.

What's been happening is the first if statement always returns true after the assignment regardless of what the className is going into the function.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 06-14-2007, 11:03 AM Re: Javascript + CSS Problem.
Super Talker

Posts: 116
Trades: 0
Definately with the above... you a double "=="

But it seems as if you can delete the first If statement as it seems repetitive with the second.

function description(element)
{
if (element.className=='description')
{
element.className='description_open';
}
else
{
element.className='description';
}
}

DonnaZ
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 06-23-2007, 06:29 PM Re: Javascript + CSS Problem. (If function, changing css).
Junior Talker

Posts: 1
Trades: 0
In your if statements you aren't comparing (==) classNames but instead assigning (=) values onto them.

Here's the function as you intended it.
Code:
function description(element){
	if (element.className=='description_open'){
		element.className='description';
	} 
	if (element.className=='description') {
		element.className='description_open';
	} else {
		element.className='description';
	}
}
Those not interested in js stop reading now.

What happened in the original function is that in each if statement a new value was assigned to the className (which isn't a failing action, hence the if is handled as being 'true'), so in reality the method just assigned another value to the className 4 times. It could never assign the "description" value because of this.
antibuzz is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Javascript + CSS Problem.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.40953 seconds with 12 queries