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
Something Easy Apparently Is Hard...
Old 12-07-2009, 08:06 PM Something Easy Apparently Is Hard...
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Hello, and again, I have a simple code that for a reason of it's own, it doesn't decide to work...

Code:
<script type="text/javascript">
function ChangeView(a,b) {
document.b.style.color=a;
}
</script>
<a class="viewbutton" onclick="ChangeView('white,historydiv');">History</a>
<div id="historydiv">
History Div
</div>
All that should do (but it doesn't) is first have a script that contains a function called ChangeView, that uses values 'a' and 'b'. Then there's a simple link that onclick, passes the values 'white' and 'historydiv' to the script, which should turn 'historydiv''s text white.


But it doesn't.


I've been at this for a long time now, all over the place, and I've gotten it to change historydiv if I specify it in the script, like this:
Code:
function ChangeView(a) {
document.historydiv.style.color=a;
}
But I want it to be able to do it from script, so I don't have to make millions of these to accommodate my million (much less) divs.

I have no idea what the problem of the code it (the first one). Am I missing some single quotes or something stupid like that? No error messages, just not working...

And BTW, there is no CSS for my historydiv. No fixed styles.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 12-07-2009 at 08:07 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-07-2009, 11:24 PM Re: Something Easy Apparently Is Hard...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Let's switch to DOM based coding and correct your single quote error on the function call (and return false on the anchor tag):

HTML Code:
<script type="text/javascript">
function ChangeView(a,b) {
  document.getElementById(b).style.color = a;
}
</script>
<a class="viewbutton" onClick="ChangeView('white','historydiv');return false;">History</a>
<div id="historydiv">
History Div
</div>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 12-08-2009, 03:37 PM Re: Something Easy Apparently Is Hard...
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Thanks, JeremyMiller! Works great! Just one (hopefully simple) question.

That wasn't my actual code. Why would I want to turn text white? My actual code is a whow/hide toggle switch. It toggles the display on a DIV.

Code:
<script type="text/javascript">
function ChangeView(a,b) {
  if (b.style.display = none) {
   document.getElementById(b).style.display = block;
  }
  else if (b.style.display = block) {
   document.getElementById(b).style.display = none;
  }
}
</script>
<a class="viewbutton" onClick="ChangeView('block','historydiv');return false;">History</a>
<div id="historydiv" style="display: none;">
Contents Of HistoryDiv
</div>
What that should do is the same thing as before, but change it to toggle display from none to block, and check whether to turn it to none or block.

In 'Programmer's English';
"If historydiv is block, turn it to none, else if it's none, turn it to block."

But it just doesn't work, another stupid quote mistake? Before, I had what you have now (I didn't post it, it didn't work anyway), but without the 'return false'. With 'return false' it works. Hmm...
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-08-2009, 03:44 PM Re: Something Easy Apparently Is Hard...
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Using a switch statement is a more efficient method than a bunch of "if" statements

http://www.modtalk.co.uk/_site/code/.../text-element/

HTML Code:
<span id="t0" onclick="toggle('h0',this.id);" style="cursor:pointer;">Show</span><span> 0</span>
<span id="t1" onclick="toggle('h1',this.id);" style="cursor:pointer;">Show</span><span> 1</span>

<div id="h0" style="visibility:hidden;display:none"> This is normally hidden</div>
<div id="h1" style="visibility:hidden;display:none"> This is normally hidden as well</div>
Code:
function toggle(id,sender){
	var e = document.getElementById(id).style;
	var t = document.getElementById(sender);
	var disp = e.display;
	var vis = e.visibility;
    var txt = t.innerHTML;
	switch(disp) 
		{
		case 'none':
			disp = 'block';
			vis =  'visible';
            txt = 'Hide';
            
			break;
		case 'block':
			disp = 'none';
			vis =  'hidden';
            txt = 'Show';
			break;
		default:
			disp = 'none';
			vis =  'hidden';
            txt = 'Show';
		}			
	e.display = disp;
	e.visibility = vis;
    t.innerHTML = txt;

	}
	
</script>
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-08-2009, 03:55 PM Re: Something Easy Apparently Is Hard...
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
OMG thanks!!!!!!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 09:08 AM Re: Something Easy Apparently Is Hard...
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
All this script does is toggle the visibility of a div when you click on a button.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-23-2009, 06:22 PM Re: Something Easy Apparently Is Hard...
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Currently yep, but you can add what ever else you need it to do to the script.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Something Easy Apparently Is Hard...
 

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.24798 seconds with 12 queries