Basically I have 6 buttons and 6 paragraphs (each one button relating to a specific paragraph). I want to show a paragraph of text when a certain button is clicked, and then hide that paragraph when the button is clicked again.
I have looked through similar questions but cannot seem to get it to work. I think its because I have only started trying to use jquery and dont really understand the problem. All hep would be appreciated! thanks!
html:
<div class="button"><div class="button float_l"><a href="#">More..</a></div>
<div class="button"><div class="button float_l"><a href="#">More..</a></div>
<div class="button"><div class="button float_l"><a href="#">More..</a></div>
<p><div id="text1">Paragraph 1</div></p>
<p><div id="text2">Paragraph 2</div></p>
<p><div id="text3">Paragraph 3</div></p>
javascript:
$(document).ready(function () {
$("#text1").hide();
$(".button").click(function(){
$("#text1").toggle();
$("#text2").hide();
$(".button").click(function(){
$("#text1").toggle();
});
the first button should relate to the first paragraph and so on. Iv tried using the 'this' function to relate to a specific button but must be using it incorrectly because it dosent work.
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.
You can write code on change or on key press event of a particular textbox to show or hide control. You can also use jquery to achieve this.
The style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document.
The :visible selector in jQuery is used to select every element which is currently visible. It works upon the visible elements. The elements that are consuming space in the document are considered visible elements. The height and width of visible elements are larger than 0.
First of all, you have to identify your different buttons, if you use the "button" class, you will always refer to the all 3 buttons.
Also you don't need to put a div element inside a p element.
So you can do something like this:
<div id="btn1" class="button"><a href="#">More..</a></div>
<div id="btn2" class="button"><a href="#">More..</a></div>
<div id="btn3" class="button"><a href="#">More..</a></div>
<p id="p1">Paragraph 1</p>
<p id="p2">Paragraph 2</p>
<p id="p3">Paragraph 3</p>
Then, javascript:
$(document).ready(function () {
$("p").hide();
$("#btn1").click(function(){
$("#p1").toggle();
});
$("#btn2").click(function(){
$("#p2").toggle();
});
$("#btn2").click(function(){
$("#p2").toggle();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With