Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> JavaScript: Hide And Show Any Element With CSS, From the simple way to the more advanced way (Javascript & CSS)
dragoneye
post May 9 2006, 12:29 AM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 2
Joined: 8-May 06
Member No.: 13,292



Welcome everyone, this is my first post.

The first thing you need to know is...

CSS has two special attributes, the first one is "display" and the second is "visibility".

The difference between these two goes like this.

"display": has many properties or values, but the ones we need are "none" and "block". "none" is like a hide value, and "block" is like show. If you use the "none" value you will totally hide what ever html tag you have applied this css style. If you use "block" you will see the html tag and it's content. very simple.

"visibility": has many values, but we want to know more about the "hidden" and "visible" values. "hidden" will work in the same way as the "block" value for display, but this will hide tag and it's content, but it will not hide the phisical space of that tag. For example, if you have a couple of text lines, then and image (picture) and then a table with three columns and two rows with icons and text. Now if you apply the visibility css with the hidden value to the image, the image will disappear but the space the image was using will remaing in it's place, in other words, you will end with a big space (hole) between the text and the table. Now if you use the "visible" value your target tag and it's elements will be visible again.

For example of what I'm saying take a look to the following images.


IPB Image
This is how it's looks if you use the "block" value for the display attribute.



IPB Image
This is how it's looks if you use the "none" value for the display attribute.



IPB Image
This is how it's looks if you use the "visible" value for the visibility attribute.



IPB Image
This is how it's looks if you use the "hidden" value for the visibility attribute.


I prefer to use the "display" atribute.

This is how it's looks the source.

HTML
<img style="display: block;" src="the_source_img_path" width="100" height="100" />


or

HTML
<img style="display: none;" src="the_source_img_path" width="100" height="100" />


Now if you want to hide or show this image using a false link or a form button please follow the following instructions.



first create the image html source...

HTML
<img id="myIMG" style="display: none;" src="the_source_img_path" width="100" height="100" />


... and place a "none" value for the display atribute and add an id element to the img source, "myIMG" for example.

Now create a false text link...

HTML
<span style="color: blue; text-decoration: underline; cursor: pointer;">Click Me</span>


now lets add an "onclick event" to that span tag

HTML
<span onclick="if(document.getElementById('myIMG').style.display=='none') {document.getElementById('myIMG').style.display='block';} else {document.getElementById('myIMG').style.display='none';}" style="color: blue; text-decoration: underline; cursor: pointer;">
Click Me
</span>


This javscript source will hide or show the image in the moment you click over the false link.

The same thing can be applied to a form button.

If you need to re-use the javascript source you can create a simple function, for example...

CODE
<script>

function changeme(id, action) {
       if (action=="hide") {
            document.getElementById(id).style.display = "none";
       } else {
            document.getElementById(id).style.display = "block";
       }
}

</script>


You can call the function using the same way as for the previous example.

HTML
<span onclick="changeme('myIMG', 'hide');" style="color: blue; text-decoration: underline; cursor: pointer;">Hide the image</span>


HTML
<span onclick="changeme('myIMG', 'show');" style="color: blue; text-decoration: underline; cursor: pointer;">Show the image</span>


Just remember to add the function at the begining of your html source inside your head tags.

Well, thats all for now, I will post more advance ways to implement this little trick (tip).

Remember, if you need help on this topic, just send me a PM or post directly in here.

Att.
v.DragonEyE.n09

P.S thank you "twitch" for the TIP

This post has been edited by dragoneye: May 9 2006, 07:11 PM
Go to the top of the page
 
+Quote Post
twitch
post May 9 2006, 05:12 PM
Post #2


Veteran Nut
Group Icon

Group: Members
Posts: 527
Joined: 4-October 05
From: UK
Member No.: 8,895



Note: Use the HTML BBCode to show HTML(XHTML) snippets by using [ html ][ /html ] (without spaces). Use the CODE BBCode to show any other code snippets (other than PHP/HTML) using the same way, but code instead of html.


Interesting article, and very well explained.

Javascript tutorial can be found at the w3schools' website (http://www.w3schools.com).
Go to the top of the page
 
+Quote Post
Quatrux
post May 9 2006, 08:37 PM
Post #3


the Q
Group Icon

Group: [HOSTED]
Posts: 1,051
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



This is interesting how you can use JS with CSS, I have learned some Javascript lately, even though I don't know where to use practically (I mean I know, but I never needed) and with time I forgot lots of things, but its not a hard language at all - I don't like it, but I need some things it offers to me.. and I always try to find alternatives to it. Nice work on making the this article, when I read it through - I got the idea. wink.gif
Go to the top of the page
 
+Quote Post
demolaynyc
post Feb 16 2007, 03:56 AM
Post #4


Premium Member
Group Icon

Group: Members
Posts: 330
Joined: 2-February 06
Member No.: 11,040



I've always wanted to try this in my future AJAX applications. Nifty trick and very helpful tutorial. Thank you.
Go to the top of the page
 
+Quote Post
mastercomputers
post Feb 17 2007, 04:19 AM
Post #5


PESTICIDAL MANIAC
Group Icon

Group: Members
Posts: 626
Joined: 1-September 04
From: Auckland, New Zealand
Member No.: 27



This BBCode:

Clicking on the title bar closes the message, clicking back on it, reopens it.

Notice from mastercomputers:
This one here


Was developed with a similar method, except it does not rely on IDs, now this is probably not the latest script I created, I still think it is using my very first implementation of this script which evolved into a more accurate and cleaner way, however it still does what it was intended to do.

Unfortunately this BBCode is for administrator/moderators use only, if caught using it could get you in trouble, though it's allowed in the test post forum.

The code for this BBCode the javascript file and method to install it into IPB is also available freely, somewhere, if it can't be found, then I'll try and locate the files on my computer and put the site for it back up.

Cheers,

MC
Go to the top of the page
 
+Quote Post
iGuest
post Feb 9 2008, 02:25 PM
Post #6


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Show and Hide
JavaScript: Hide And Show Any Element With CSS

Dear,
How to show and on click function a drop down menu, with smooth animation in javascript.

Thanks

-praveen
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting Screen Resolution using Javascript.(8)
  2. How To Create A Popup Window With Javascript?(19)
  3. javascript vs java(12)
  4. Javascript: Disable Mouse Right-click In Browser(16)
  5. Best Way To Learn Javascript(9)
  6. Javascript: Text To Texbox And Back To Text(2)
  7. Unobtrusive Javascript Image Rollovers(0)
  8. Dynamicdrive: Good Site For JavaScript Codes(5)
  9. Downloads With Javascript?(7)
  10. Javascript: Simple Slidedown Menu(2)
  11. JavaScript: Simple Dropdown Menu(1)
  12. Calling Of Functions Between Mulitple External Javascript Files(2)
  13. Javascript: Browser Detection Script(0)
  14. Ever Needs To Find Out A Table Height Or With With JavaScript(2)
  15. Create And Import JavaScript Modules For A Large Script(2)
  1. Vertical Marquee Using JavaScript(0)
  2. JavaScript Frames & Querystring(4)
  3. JavaScript Off Redirect Script(2)
  4. I Need Help With Javascript.(7)
  5. Problems With Dynamically Loading Javascript(2)
  6. Add Text To Textarea(6)
  7. Javascript Question(4)
  8. Javascript: How Do I Create Embedded Pop-up Windows?(7)
  9. Problem With Javascript Alert();(9)
  10. Include Function For Javascript(7)
  11. Fun With Javascript And Forms(2)
  12. Javascript Help Needed : Alert(z) Works Fine But Document.write Not(2)
  13. Javascript Changes Aren't Working.(6)


 



- Lo-Fi Version Time is now: 11th October 2008 - 07:52 AM