There are certain cases when you need to hide & show divs on basis of some click event ! Here is one of the basic usage of JavaScript & jQuery to show & hide divs on click of radio buttons.
Checkout in action
Checkout above video for step by step guide for both native JavaScript and jQuery code.
https://www.youtube.com/c/CodeCanvas/ – Please checkout my YouTube channel and subscribe for more technical videos and tutorials.
Please leave your comments , if you found above code useful. so , I can improve it from your suggestions.
Thanks for this. It helped me out a lot. My problem was a variation on this. I wanted to control 1 div from a radio button group. Here is how I modified it
http://code.jquery.com/jquery-latest.js
$(document).ready(function () {
$(‘#MoreInfoHide’).click(function () {
$(‘#dvMoreInfo’).hide(‘fast’);
});
$(‘#MoreInfo’).click(function () {
$(‘#dvMoreInfo’).show(‘fast’);
});
});
Here is my HTML
Yes
No
If someone answered no I wanted to ask for more information
LikeLike
nice, this is helpful
Thanks
LikeLike
thank you!!! its very helpful to me…specially i m not a java script developer…its work perfectly…:)
LikeLiked by 1 person
Block of Showing Item should be auto hidden by that when a radio option selected then only one item should be shown here…
LikeLike
So ?
LikeLike
Hi, i have a bit of trouble trying to implement this in a table. Would you mind telling me what is wrong with it? I have 4 radio buttons, where if one clicked, a few rows should be shown based on it. I think i may have messed up the coding or I may have put it wrongly. Will you please explain to me in detail as I’m a beginner in this >< Also, how do you collaborate this with php(pdo) if I want to take the inputs to the database?
LikeLike
Hi, Can you please show me your code ? what you have tried so far ? What trouble you are facing ?
LikeLike
Okay, i have cleaned up my code but the problem is when i tried to put another row into the div, the dreamweaver will either give me an error on my wrapper or the itself. My code is long, am i suppose to put here?
LikeLike
Please contact me on my gmail : cemilap.88@gmail.com
LikeLike
hello sir,
can you please load the div to only one select of radio button ?
LikeLike
Hi devraj, Please check http://jsfiddle.net/3ZnGj/859/ , Is this what you are looking for ?
LikeLike
nice dude , thanks
LikeLiked by 1 person
Super! Really help me!
LikeLike
Dude,
this code works in chorome, safari but not in firefox
Please gimme an idea
LikeLike
I have tested this (http://jsfiddle.net/3ZnGj/3/) code in Firefox & its working fine..
LikeLike
useful dude..thaxz
LikeLike
Very nice code..This is very helpful..I added you on facebook. Nice to meet you 🙂
LikeLike
Your script is very fine. But there is some improvement needed. When the page loads both divs should be hidden. In your script both are visible on page load and when radio button a selected then respective dive content is shown.
LikeLike
Thanks for your suggestion.
Here is your suggested output. http://jsfiddle.net/94mW8/
LikeLike
This is very good!!!
Thank you very much!
LikeLike
You are the SAVIOUR , GOD BLESS you Sir
LikeLike
this is very helpful THANK YOU
LikeLike
Your welcome. 🙂
LikeLike
Hi!! This is very helpful to me. Thanks…. 🙂
LikeLike
Your welcome. 🙂
LikeLike
please keep a demo , i thing its better way
LikeLike
Hey Rohit,
check out my code working at http://jsfiddle.net/3ZnGj/3/
Enjoy..!!!
LikeLike
thanks its really use full
LikeLike
greate tutorials!
what if I want the two different forms inside those div to display=none
and display=block again when it’s clicked?
LikeLike
Hey Daroath,
Thanks for your feedback.
I think you can assign unique id to each form and inside each click event, you can add logic to show and hide each form inside div..!!
LikeLike
both div show on the web browser at the same time at first when loaded how can i resolve it to show the first div
LikeLike
Hello Tunji,You can write as below.
$(document).ready(function () {
$(‘#div1’).show(‘fast’);
$(‘#div2’).hide(‘fast’);
});
This will show only first div on page load. After this you can put click event.
LikeLike
its really nice buddy
LikeLike
Hi, Thanks again for this example.
Would the same concept be possible but with the use of a table to hide and show certain rows with the use of a radio button?
Thanks
LikeLike
Thanks for your feedback..
Yes, you can give id=”div1″ & id=”div2″ to individual rows of table.
That’s it.
LikeLike
Hey Milap,
Thank you buddy…!
In my div2 I have check box selection , when I submit form with radio_1 it taking div2 checked items also .How can I limit this …..?
LikeLike
Radio button & check-box both are 2 different entities , You will get both value in POST when submit form..
LikeLike
Is it possible to have all of the dynamic divs hidden when the page loads? I only want the text to show if a radio button is clicked. Thanks for the script!
LikeLike
Yes Paul , you can hide both divs on page load.
Just add below line on load function.
$(‘#div1’).hide();
$(‘#div2’).hide();
You are done. Enjoy..!!
LikeLike
and where is that actually?
where should i add this lines:
$(‘#div1′).hide();
$(‘#div2′).hide();
so the divs dont show on pageload?
LikeLike
You can write like this :-
$(document).ready(function() {
$(‘#div1′).hide();
$(‘#div2′).hide();
}
This will hide both divs on pageload…
LikeLike
Thanks buddy! this is helpful..!!
LikeLike