Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-21-2007, 09:19 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Hiding/Showing a Div with JavaScript.

What action do I need to use to hide or show a div, and everything it contains. I know that it needs a seperate ID, and how to call the function, but I need to know the action.

My current way of going about this(without the action of course).
Code:
<script type="text/javascript">
hcur=0

function hidelast(){
switch(hcur){
case 0:
    hide div0
    break
case 1:
    hide div1
    break}
}

function header_main(){
if ( hcur!=0 ){
    hidelast()
    hcur = 0
    show div0}
}

function header_diff1(){
if ( hcur!=1 ){
    hidelast()
    hcur = 1
    show div1}
}
</script>

...

<div id="div0">
    <a href="url"><img onmouseover="header_main()"></a>
</div>
<div id="div1">
    <a href="url"><img onmouseover="header_diff1()"></a>
</div>
I'm asking for just the action on hiding/showing a div, but if anyone can see something obvious wrong with my logic please, point that out too, this is my first time writing JS. This is intended for about 5 divs.
Reply With Quote
  #2  
Old 10-21-2007, 09:48 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

For you div code, you need to add the display style like "<div id=xxx style="display: none;">"
Reply With Quote
  #3  
Old 10-21-2007, 10:07 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So I would use...
Code:
document.getElementById('div1').style.display="none"
and
Code:
document.getElementById('div1').style.display="inline"
?

EDIT: This works for the hiding, but not for the showing. I tried visibility, same thing.
Reply With Quote
  #4  
Old 10-21-2007, 11:57 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This is the javascript I use:
PHP Code:
function collapse_news(id)
{
    var 
collapseText document.getElementById('k' id);
    var 
collapseBild document.getElementById('pic' id); 

    if (
collapseText.style.display == 'none') {
          
collapseText.style.display 'block';
          
collapseBild.src '/forums/images/buttons/collapse_alt.gif';
    }
    else {
          
collapseText.style.display 'none';
          
collapseBild.src '/forums/images/buttons/collapse_alt_collapsed.gif';
    }

And then this:

PHP Code:
<a href="javascript:collapse_news('220')"><img border=0 src="/forums/images/buttonsttd/collapse_alt.gif" id=pic220 alt="Show / Hide"Date and Title of my news</a><div id=k220 style="display: none;">My News Hidden</div
Reply With Quote
  #5  
Old 10-22-2007, 08:08 PM
Analogpoint's Avatar
Analogpoint Analogpoint is offline
 
Join Date: Feb 2007
Posts: 656
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

To hide, set display to "none" and to show, set display to "" (an empty string.)
Reply With Quote
  #6  
Old 10-22-2007, 08:16 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you, I made this function modeled after that. However it's not working, is my syntax wrong?

hcur stores the id of the current element shown.
hx tells how many additional elements are included. (if theres more than one)

hcur + 'b' is the button, it changes images. hcur + '2', and hcur + '3' are additional elements that should be hidden/shown if needed.

Code:
function switchheader(id, x)
{
	if ( hcur != id ){
		document.getElementById(hcur).style.display = 'none';
		document.getElementById(hcur + 'b').src = '/styles/7M-v1/new/' + hcur + '.png';
		if (hx != 1){
			document.getElementById(hcur + '2').style.display = 'none';
			if (hx == 3){
				document.getElementById(hcur + '3').style.display = 'none';}
		}
		hcur = document.getElementById(id);
		document.getElementById(hcur).style.display = 'block';
		document.getElementById(hcur + 'b').src = '/styles/7M-v1/new/' + hcur + '_alt.png';
		if (x != 1){
			document.getElementById(hcur + '2').style.display = 'block';
			if (hx == 3){
				document.getElementById(hcur + '3').style.display = 'block';}
		}
		hx = x;
            }
}
Code:
	<img onMouseover="switcheader('main', 2)" src="/styles/7M-v1/new/main.png" />
       <img onMouseover="switcheader('projects', 1)" src="/styles/7M-v1/new/projects.png" /><td>
If anyone were to tell me what i'm doing wrong that would be greatly appreciated.

PS: Saw your message just now analog i'll make that change, however this code above isn't working at all.
Reply With Quote
  #7  
Old 10-22-2007, 09:51 PM
Analogpoint's Avatar
Analogpoint Analogpoint is offline
 
Join Date: Feb 2007
Posts: 656
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

To begin with, hcur isn't defined in the function. I didn't get any further than that. But why make it that complicated? Put your two alternate headers within a div for each one, and then do something like this. Your two divs have id's headerone and headertwo.

Code:
function toggle_header()
{
      var el = document.getElementById('headerone');
      if (el.style.display == 'none')
      {
              el.style.display = '';
              document.getElementById('headertwo').style.display = 'none';
      }
      else
      {
              el.style.display = 'none';
              document.getElementById('headertwo').style.display = '';
      }
}
Reply With Quote
  #8  
Old 10-22-2007, 10:48 PM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That wouldn't work. I'm switching between about 6 divs, and it seems wasteful to check the state of each div when I can just use a variable.

I simplified it, sorry, kinda used to going overboard when coding (other language). Really don't like to use specifics.

Code:
var hcur='logo';
function switchheader(id){
	if ( hcur != id ){
		document.getElementById(hcur).style.display = 'none';
		document.getElementById(hcur + 'b').src = '/styles/7M-v1/new/hb_' + hcur + '.png';
		document.getElementById(id).style.display = '';
		document.getElementById(id + 'b').src = '/styles/7M-v1/new/hb_' + hcur + '_alt.png';
		if (hcur == 'logo'){
			document.getElementById('logo2').style.display = 'none';}
		if (id == 'logo'){
			document.getElementById('logo2').style.display = '';}
		hcur = id;
	}
}
I'll explain exactly what i'm trying to do.
  • hcur is the last id shown, the one currently being shown.
  • id the the new one in question.
  • buttons switch to a alternate version when they are hovered on as well. Their id is the same as the div, with a b added on (id + 'b').

If the new id is not equal to the current id (why switch to the same?), then continue with the switch, else do nothing. Hide the current div, and switch the current button back to default. Then show the new div, and switch the new button to alternate.

If the past id was logo, hide the extra div. If the new id is logo, show the extra div. Set hcur to the new id as it is now the current, and end function.
Reply With Quote
  #9  
Old 10-23-2007, 03:50 AM
Analogpoint's Avatar
Analogpoint Analogpoint is offline
 
Join Date: Feb 2007
Posts: 656
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

After a quick glance, it looks good. Try putting an alert('x'); after each line to identify where it chokes.
Reply With Quote
  #10  
Old 10-24-2007, 12:39 AM
MrApples MrApples is offline
 
Join Date: Aug 2007
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ok, I did that. My code exactly for that...
Code:
var hcur='logo';
function switchheader(id){
	alert('runs! id:' + id + ' hcur:' + hcur);
	if ( hcur != id ){
	alert('1');
		document.getElementById(hcur).style.display = 'none';
		alert('2');
		document.getElementById(hcur + 'b').src = '/styles/7M-v1/new/hb_' + hcur + '.png';
		alert('3');
		document.getElementById(id).style.display = '';
		alert('4');
		document.getElementById(id + 'b').src = '/styles/7M-v1/new/hb_' + hcur + '_alt.png';
		alert('5');
		if (hcur == 'logo'){
		alert('6');
			document.getElementById('logo2').style.display = 'none';}
			alert('7');
		if (id == 'logo'){
		alert('8');
			document.getElementById('logo2').style.display = '';}
			alert('9');
		hcur = id;
		alert('10');
	}
}
Apparently it is stopping at
Code:
		document.getElementById(hcur + 'b').src = '/styles/7M-v1/new/hb_' + hcur + '.png';
Does JS lack failsafe checking? That image does not exist (haven't gotten to that yet), so is it just killing the function over it? Thank you a lot for this error finding command by the way.

PS: THe problem defiantly lies in those 2 lines, I commented them out and it works.
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04277 seconds
  • Memory Usage 2,271KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (9)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete