I am using Simple-Expand http://redhotsly.github.com/simple-expand/ to expand and collapse sections in the HTML page.
JQuery code :
$(function () {
$('.hello').simpleexpand();
});
$(function () {
$('.bye').simpleexpand();
});
HTML :
<a href='' id='hello' class='hello'> Hi </a>
<a href='' id='bye' class='bye'> Bye </a>
Div :
<div class="content">
Hello
</div>
<div class="content">
Bye
</div>
When i click on the link with the class hello, Only the DIV tag with the text Hello should be called, and when i click on the link Bye, the DIV tag with the text Bye should be displayed. How can i do this ?
It is all in the doc you posted : http://sylvain-hamel.github.io/simple-expand/
You have to specify options in your call to the simpleexpand method
For example :
$(function () {
$('.hello').simpleexpand({'defaultTarget': '.helloDiv'});
});
$(function () {
$('.bye').simpleexpand({'defaultTarget': '.byeDiv'});
});
and your html :
<div class="helloDiv">
Hello
</div>
<div class="byeDiv">
Bye
</div>
try this:
JQuery code :
$(function () {
$('a').click(function(){
var id = $(this).attr('id');
var divId = 'div_' + id;
$('#'+divId).simpleexpand();
});
});
HTML :
<a href='' id='hello'class='hello'> Hi </a>
<a href='' id='bye' class='bye'> Bye </a>
Div :
<div class="content" id='div_hello'>
Hello
</div>
<div class="content" id='div_bye'>
Bye
</div>
Related
I have a loop of posts in wordpress and I am trying to call a jquery on click show event which will show a contact form for that post when clicked.
The onclick show works only for the first post in the loop where it will display the contact form for that post no problem - but, it does not work for any of the other posts in the loop. Anyone know why this may be? The page with the post loop is https://www.salusa.co.uk/specialist-training-courses/.
The code is roughly:
<div class="archive-posts-loop">
<div class="post">
<div class="enquire">
<a class="show-form">Contact Provider</a>
</div>
<div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(
function(){
$(".show-form").click(function () {
$("#contact-form-wrapper").show("fast");
});
$("#close").click(function () {
$("#contact-form-wrapper").hide("fast");
});
});
</script>
</div><!--post-->
<div class="post">
<div class="enquire">
<a class="show-form">Contact Provider</a>
</div>
<div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(
function(){
$(".show-form").click(function () {
$("#contact-form-wrapper").show("fast");
});
$("#close").click(function () {
$("#contact-form-wrapper").hide("fast");
});
});
</script>
</div><!--post-->
</div><!--archive-post-loop-->
As you have a new form for each post you need to target the correct form for the post. You can listen for delegated click events and show/hide the nested form accordingly. Note that you do not need (nor should have) the script repeated for each form as shown in your example.
$(function(){
$(".post")
.on("click", ".show-form", function() {
$(this).parents(".post").find(".contact-form-wrapper").show("fast");
})
.on("click", ".close", function() {
$(this).parent().hide("fast");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="post">
<div class="enquire">
<a class="show-form">Contact Provider</a>
</div>
<div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;">
[Form here] close
</div>
</div>
<div class="post">
<div class="enquire">
<a class="show-form">Contact Provider</a>
</div>
<div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;">
[Form here] close
</div>
</div>
<div class="post">
<div class="enquire">
<a class="show-form">Contact Provider</a>
</div>
<div class="contact-form-wrapper" id="contact-form-wrapper" style="display:none;">
[Form here] close
</div>
</div>
You are using multiple elements with the same id of contact-form-wrapper. Instead you need to make each element have a unique id, and then select that id in the respective jquery calls. Jquery will return the first matching element given a selector, which is why it only selects the first post each time.
Here is a codepen that does what you want.
https://codepen.io/abidhasan/pen/wpdoyO?editors=1111
This is the jQuery that you can use - basically look for the sibling and change its style
$(".post .show-form").on("click", function() {
$(this).parent().siblings()[0].style.display= "block";
});
$(".post .hide-form").on("click", function() {
$(this).parent().siblings()[0].style.display= "none";
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
$(window).load(function () {
var divs = $( #add_new, #details");
$("li a").click(function () {
$(divs).hide();
$("#" + $(this).attr("class")).show();
});
});
<ul>
<li><a class="add_new" href="javascript:void(0);" >Add new</a></li>
<li><a class="details" href="javascript:void(0);" >details</a></li></ul>
<div id="add_new">
<p>ADD NEW</P>
<input type="button" id="addsettings" value="Add Informations">
</DIV>
<div id="details">
<p>detailssss</p>
</div>
<script>
$(function () {
$("#addsettings").click(function () {
$.ajax({
url: "manage.php",
success: function (html) { // this happens after we get results
$(".result").empty();
$(".result").show();
$(".result").append(html);
}
});
});
});
</script>
manage.php
<div id="div1">
pppppp
lll
kkk
</div>
<div id='div2'>
tttttt
gg
mm
</div>
<script>
$("#add_new").hide();
</script>
I need the page to get refreshed and then show the particular div tag. Now it is showing the div without page reload. For example, if i click the button inside add_new, it will take me to another page via ajax. In that another page, i have many divs (div1, div2) shown and I hide the add_new div here. Then if I click the 'details' link, and then go back to add_new again, it is still showing that divs which are in ajax page (div1, div2) not `add_new.
Please help me. I need the add_new div here.
Thanks
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(window).load(function () {
var divs = $('#add_new,#details');
$("li a").click(function () {
divs.css('display','none');
$("#" + $(this).attr("class")).css('display','block');
});
});
</script>
<ul>
<li><a class="add_new" href="javascript:void(0);" >Add new</a></li>
<li><a class="details" href="javascript:void(0);" >details</a></li></ul>
<div id="add_new" style="display: none">
<p>ADD NEW</P>
<input type="button" id="addsettings" value="Add Informations">
</DIV>
<div id="details" style="display: none">
<p>detailssss</p>
</div>
The code below does this. When I click the button for the first time, it shows the respective text. And when I click the same button again, it fades out and fades in. However, I want the respective text to disappear if the button is clicked the second time instead of fading out and then fading in.
$(document).ready(function(){
$('.info').click(function(){
$(this).next().fadeOut("slow");
$(this).next().fadeIn("slow");
});
});
HTML:
<div>
<a class="info" p style="font-size:30px" href="javascript:void(0);">header 1</a>
<h1 class="infoText" style="display:none">text 1</h1>
</div>
<div>
<a class="info" href="javascript:void(0);">header 2</a>
<h1 class="infoText" style="display:none">text 2</h1>
</div>
<div>
<a class="info" href="javascript:void(0);">header 3</a>
<h1 class="infoText" style="display:none">text 3</h1>
</div>
It should start like this:
header1
header2
header3
When I click header1, it should be like this:
header1
text1
header2
header3
And when I click header 1 again, it should be like this:
header1
header2
header3
Anybody know how to do this?
You can use fadeToggle() to toggle the display with fade effect
$(document).ready(function() {
$('.info').click(function() {
$(this).next().fadeToggle("slow");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<a class="info" p style="font-size:30px" href="javascript:void(0);">header 1</a>
<h1 class="infoText" style="display:none">text 1</h1>
</div>
<div>
<a class="info" href="javascript:void(0);">header 2</a>
<h1 class="infoText" style="display:none">text 2</h1>
</div>
<div>
<a class="info" href="javascript:void(0);">header 3</a>
<h1 class="infoText" style="display:none">text 3</h1>
</div>
You can use toggle() for this purpose. It will hide the element if it is already shown, otherwise it will shows the element.
$('.info').click(function(){
$(this).next().toggle("slow");
});
You can use toggle() Function:
$(document).ready(function() {
$('.info').click(function() {
$(this).next().fadeToggle("slow");
});
});
DEMO
$('.info').click(function(){
//$(this).next().fadeOut("slow");
$(this).next().toggle("slow");
});
Use toggle to hide and show something
AS per suggestion of #Tushar
$('.info').click(function(){
//$(this).next().fadeOut("slow");
$(this).next().fadeToggle("slow");
});
You Try this code
$(document).ready(function() {
$('.info').click(function() {
var _self = $(this);
if(_self.hasClass("active")) {
_self.next().fadeOut("slow");
_self.removeClass("active");
} else {
_self.next().fadeIn("slow");
_self.addClass("active");
}
});
});
Working Demo
I have the following html code:
<div class="slide-button" data-content="panel1">
<p><span id="panel-icon">+</span>Test1</p>
</div>
<div id="panel1" style="display: none">
<p> Test jquery menu1 </p>
</div>
<div class="slide-button" data-content="panel2">
<p><span id="panel-icon">+</span>Test2</p>
</div>
<div id="panel2" style="display: none">
<p> Test jquery menu2 </p>
</div>
and the following jquery code:
$(".slide-button").on('click', function(){
var panelId = $(this).attr('data-content');
$('#'+panelId).toggle(500);
});
The toggle itself works perfectly.
However, I also want to include a plus and minus sign as you can see from my span-tag in the html code.
What do I have to add to my existing jquery code in order to change the + and - sign when the user clicks on the link?
Thank you for any help :-)
if you change it to a class
<div class="slide-button" data-content="panel1">
<p><span class="panel-icon">+</span>Test1</p>
</div>
<div id="panel1" style="display: none">
<p> Test jquery menu1 </p>
</div>
you could do
$(".slide-button").on('click', function(){
var panelId = $(this).attr('data-content');
$('#'+panelId).toggle(500);
$(this).find('.panel-icon').text(function(_, txt) {
return txt === "+" ? "-" : "+";
});
});
FIDDLE
a play on a similar question asked by me that was graciously answered.
three divs, all hidden. jquery toggles between them via different links. this works great! now, i'd like to clink the link corresponding to the active div and hide it, esentially the same as when the page loads with all of them hidden. right now it fades out and back in.
help! thank you!
HTML:
<div id="nav">
<a class="home" id="show_about" title="About">ABOUT</a><br />
<a class="home" id="show_subscribe" title="Subscribe">SUBSCRIBE</a><br />
<a class="home" id="show_contact" title="Contact">CONTACT</a>
</div>
<div id="content">
<div class="current" id="about">
<p>ABOUT's content</p>
</div>
<div id="subscribe">
<p>SUBSCRIBE's content</p>
</div>
<div id="contact">
<p>CONTACT's content</p>
</div>
</div>
Javascript:
$(function(){
$('#about').css('display', 'none');
$('#subscribe').css('display', 'none');
$('#contact').css('display', 'none');
});
$('.home').click(function(){
var id = $(this).html().toLowerCase();
$('.current').fadeOut(600, function(){
$('#'+id).fadeIn(600);
$('.current').removeClass('current');
$('#'+id).addClass('current');
});
});
Try this out..
Since on fadeOut the current class is removed, if the size of current is 0 it means nothing is selected. We can simply fadeIn the content div.
$('#about, #subscribe, #contact').hide();
$('.home').click(function() {
var id = $(this).html().toLowerCase();
var $content = $('#' + id + ':not(:visible)');
if ($('.current').length === 0) {
showContent($content)
}
else {
$('.current').fadeOut(600, function(){showContent($content)});
}
});
function showContent(content) {
content.fadeIn(600);
$('.current').removeClass('current');
content.addClass('current');
}
Example on jsfiddle.
Here is another approach:
$(function(){
$('#content div').hide();
});
$('.home').click(function(){
var targetSelector = '#' + $(this).attr('title').toLowerCase();
var targetShown = $(targetSelector).is(':visible');
if (targetShown) {
$(targetSelector).fadeOut(600);
} else {
$('#content div:not(' + targetSelector + ')').fadeOut(600,
function() {$(targetSelector).fadeIn(600)});
}
});
Check to see if the id of the clicked element is equal to the id of the element currently being displayed (i.e. the element with the current class). If it is a different element, then a swap needs to take place. If it is the same element, then it needs to be hidden and have its current class removed...
(Edit: fixed code)
HTML:
<div id="nav">
<a class="home" id="show_about" title="About">ABOUT</a><br />
<a class="home" id="show_subscribe" title="Subscribe">SUBSCRIBE</a><br />
<a class="home" id="show_contact" title="Contact">CONTACT</a>
</div>
<br />
<br />
<br />
<div id="content">
<div id="about">
<p>ABOUT's content</p>
</div>
<div id="subscribe">
<p>SUBSCRIBE's content</p>
</div>
<div id="contact">
<p>CONTACT's content</p>
</div>
<div class="current" id="dummy"></div>
</div>
JS:
$().ready(function()
{
$('#about').hide();
$('#subscribe').hide();
$('#contact').hide();
$('#dummy').hide();
$('.home').click(function() {
var id = $(this).html().toLowerCase();
if ($('.current').length >0) {
if($('.current')[0].id.toLowerCase() != id)
{
$('.current').hide();
$('.current').removeClass('current');
$('#'+id).addClass('current');
$('#'+id).show();
}
else
{
$('.current').hide();
$('.current').removeClass('current');
$('#dummy').addClass('current');
}
}
});
});
Just replace the .hide()'s and .shows()'s with fades in and out.