I am trying to hide all elements when one button is clicked and only the menu belonging to that specific button should appear.
If I do like this that will be too long if more buttons and menus are there.
Are there any methods that can shorten this and get the desired result?
$(document).ready(function(){
$("#steps").click(function(){
$("#calculateMenu").hide();
$("#stepsMenu").show();
});
$("#calculate").click(function(){
$("#stepsMenu").hide();
$("#calculateMenu").show();
});
});
You could do it like this:
$("#steps,#calculate").click(function() {
var id = $(this).attr("id");
$("[id*=Menu]").hide();
$("#" + id + "Menu").show();
});
All element where id contains Menu will be hidden, and then we will show the "correct" element
Demo
$("#steps,#calculate").click(function() {
var id = $(this).attr("id");
$("[id*=Menu]").hide();
$("#" + id + "Menu").show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="steps">steps</button>
<button id="calculate">calculate</button>
<div id="stepsMenu">stepsMenu</div>
<div id="calculateMenu">calculateMenu</div>
When one button is clicked and the only menu belonging to that specific
button should appear
As #freedomn-m 's comment,
You can use data-attributes to link between button and menu combined with class selector like below:
$(".Mybutton").click(function() {
var activeMenu = $(this).data('buttonname');
$(".menu").hide();
$("#" + activeMenu).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='Mybutton' data-buttonname='steps'>steps</button>
<button class='Mybutton' data-buttonname='calculate'>calculate</button>
<button class='Mybutton' data-buttonname='finish'>finish</button>
<div class='menu' id="steps">stepsMenu</div>
<div class='menu' id="calculate">calculateMenu</div>
<div class='menu' id="finish">finishMenu</div>
Related
I have a function that assigns dynamic classes to my div's. This function is a that runs on the page. After the page loads, all 10 of my primary 's have classes ".info1" or ".info2" etc...
I am trying to write a Jquery function that changes the class of the div you click on, and only that one. Here is what I have attempted:
$(".info" + (i ++)).click(function(){
$(".redditPost").toggleClass("show")
});
I have also tried:
$(".info" + (1 + 1)).click(function(){
$(".redditPost").toggleClass("show")
});
And
$(".info" + (i + 1)).click(function(){
$(".redditPost").toggleClass("show")
});
EDITED MY HTML: DIV RedditPost is actually a sibling to Info's parent
<div class="listrow news">
<div class="newscontainer read">
<div class=".info1"></div>
<div class="redditThumbnail"></div>
<div class="articleheader read">
</div>
<div class="redditPost mediumtext"></div>
</div>
My issue is two fold.
The variable selection for ".info" 1 - 10 isn't working because i doesn't have a value.
If I did target the correct element it would change all ".redditPost" classes instead of just targeting the nearest div.
Try like this.
$("[class^='info']").click(funtion(){
$(this).parent().find('.redditPost').toggleClass("show");
});
Alternative:
$('.listrow').each(function(){
var trigger = $(this).find("[class^='info']");
var target = $(this).find('.redditPost');
trigger.click(function(){
target.toggleClass("show");
});
});
Try this
$("div[class*='info']").click(function(){
$(this).parent().find(".redditPost").toggleClass("show")
});
Explanation:
$("div[class*='info'])
Handles click for every div with a class containing the string 'info'
$(this).parent().find(".redditPost")
Gets the redditPost class of the current clicked div
Since the class attribute can have several classes separated by spaces, you want to use the .filter() method with a RegEx to narrow down the element selection as follows:
$('div[class*="info"]').filter(function() {
return /\binfo\d+\b/g.test( $(this).attr('class') );
}).on('click', function() {
$(this).siblings('.redditPost').toggleClass('show');
});
.show {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="listrow news">
<div class="newscontainer read">
<div class="info1">1</div>
<div class="redditThumbnailinfo">2</div>
<div class="articleheader read">3</div>
<div class="redditPost mediumtext">4</div>
</div>
</div>
I fetch posts from database with php while loop, Here is the e.g of HTML code->
<div id="1"> content <button class="btn-primary" data-id="1">button</button></div>
<div id="2">content 2 <button class="btn-primary" data-id="2">button</button></div>
if someone click on first button then div with id=1 should be removed
Here is the jquery code
$(function() {
$("body").on("click", ".btn-primary", function() {
var ids = $(this).data('id'); // get data-id atrribute
var elements = this;
$('#id').remove(); // How can i remove the div by showing each id here
$.ajax({
type: "POST",
url: "https://www.example.com/ajax,
data: "ids=" + ids,
success: function(data) {
setTimeout(function() {
$('.conner').append(data).fadeIn('slow');
}, 2000);
}
});
});
});
How can Get div id dynamically and remove the div on click
Unless data-id holds several values you can:
var ids = $(this).data('id'); // get data-id atrribute
$('#' + ids).remove();
More simple way - is to remove a button parent:
$( this ).parent().remove();
Either use the variable like :
$('#'+ids).remove();
Or you can simply go with
$(this).parent().remove();
If your container is always div you don't need to use ids you could just use :
$(this).closest('div').remove();
Hope this helps.
sometimes it happend that if id of an element contains only number and we try to access that element with that id its creating problem soe better to use
$(this).parent().remove();
If div which you want to delete is immediate parent of button than you can use,
$(this).parent().remove();
If you have hierarchy than you can add .parent() multiple time like
$(this).parent().parent().parent().remove();
Is it something like this ??
Codepen example
$(".button1").click(function() {
$(this).closest('.div1').fadeOut(500);
});
HTML
<div class="div1">content1
<button class="btn-primary button1">button for div 1</button>
</div>
<div class="div1">content2
<button class="btn-primary button1">button for div 2</button>
</div>
<div class="div1">content3
<button class="btn-primary button1">button for div 3</button>
</div>
<div class="div1">content4
<button class="btn-primary button1">button for div 4</button>
</div>
<div class="div1">content5
<button class="btn-primary button1">button for div 5</button>
</div>
i have some divs i wanna toggle i am able to toggle but unable to remove classes when ever i click on the next div
index.php
<div id="menu_top1" class="menu_top1">LINK 1</div>
<div id="menu_top" class="menu_top">LINK 2</div>
<div id="content_area1" style="display:none;">
THIS IS LINK 1
</div>
<div id="content_area2" style="display:none;">
THIS IS LINK 2
</div>
Jquery.js
$('#menu_top1').click(function() {
$('#content_area1').toggle('slow', function() {
$('.menu_top1').toggleClass('active');
});
});
Here is a Fiddle https://fiddle.jshell.net/kunz/t5u6mcmn/
if you are trying to show corresponding content_area when you click on link and make the link active? you can check this
fiddle
i saw you using same id for multiple elements.just edited them.
still you want same id for all elements(strictly not recommended) check this fiddle
check this updated fiddle
$('.menu_top').click(function() {
var index = $( this ).index() + 1;
console.log(index);
$('[id^="content_area"]' ).hide();
$('#content_area' + index ).toggle('slow', function() {
$('.menu_top').toggleClass('active');
});
});
I am working on a site where I have 4 expandable divs using jquery's slide toggle. All works fine but I don't want more than 1 of those divs expanded at a time. In other words: when a div is expanded, other divs that are expanded should close. Accordion isn't an option I think because all the div's styling is different. Below is my code.
$(document).ready(function(){
// slidetoggles
$("#tgx-window").hide();
$("#tgx-button").show();
$("#tgs-window").hide();
$("#tgs-button").show();
$("#tgm-window").hide();
$("#tgm-button").show();
$("#tgl-window").hide();
$("#tgl-button").show();
$('#tgx-button').click(function(){
$('#tgx-button').toggleClass('closebutton');
$("#tgx-window").slideToggle();
});
$('#tgs-button').click(function(){
$('#tgs-button').toggleClass('closebutton');
$("#tgs-window").slideToggle();
});
$('#tgm-button').click(function(){
$('#tgm-button').toggleClass('closebutton');
$("#tgm-window").slideToggle();
});
$('#tgl-button').click(function(){
$('#tgl-button').toggleClass('closebutton');
$("#tgl-window").slideToggle();
});
HTML:
<a onclick="" class="show_hide" id="<?=strtolower($service['title']);?>-button"></a>
<div class="slidingDiv" id="<?=strtolower($service['title']);?>-window">
<?
$infoBox = '<h1>'.$service['subtitel'].'</h1>';
$infoBox .= $service['description'];
echo replaceTags($infoBox);
?>
</div>
It's a bit hard to answer this question without knowing your HTML but here is a solution I hope;
HTML (I presume):
<div id="tgx-window">tgx window</div>
<div id="tgs-window">tgs window</div>
<div id="tgm-window">tgm window</div>
<div id="tgl-window">tgl window</div>
<button id="tgx-button">Show tgx</button>
<button id="tgs-button">Show tgs</button>
<button id="tgm-button">Show tgm</button>
<button id="tgl-button">Show tgl</button>
JavaScript:
jQuery(function($) {
var $windows = $('#tgx-window,#tgs-window,#tgm-window,#tgl-window'),
$buttons = $('#tgx-button,#tgs-button,#tgm-button,#tgl-button');
$windows.hide();
$buttons.on('click', function(e) {
var $id;
e.preventDefault();
$buttons.removeClass('closebutton');
$id = $('#' + this.id.split('-')[0] + '-window');// Get window id
$windows.slideUp();
if(! $id.is(':visible') ) {
$id.slideDown();
$(this).addClass('closebutton');
}
});
});
You can see a working example here.
Use class instead of id of button and probably its parent .tgx-window
$('.tgx-button').click(function(){
$(this).toggleClass('closebutton');
$(this).closest(".tgx-window").slideToggle();
});
I have problem in hide and show the div element.
In this scenario when user click on the year the respect content is shown.
Problem I want to inactive hyperlinking on respective year when it is opened.
The script and html is below;
for this I have tried .preventDefault(). but not got any success:
<script type="text/javascript" >
$(document).ready(function() {
$("div.new:gt(0)").hide();// to hide all div except for the first one
$("div[name=arrow]:eq(0)").hide();
// $("div.nhide:gt(0)").hide();
// $("a[name=new]").hide();
$("a[name=new]").hide();
$('#content a').click(function(selected) {
var getID = $(this).attr("id");
var value= $(this).html();
if( value == '<< Hide')
{
// $("#" + getID + "arrow").hide();
$("a[name=new]").hide();
$("#" + getID + "_info" ).slideUp('slow');
$("div[name=arrow]").show();
$("div.new").hide();
$(this).hide();
// var getOldId=getID;
// $("#" + getID ).html('<< Hide').hide();
}
if($("a[name=show]"))
{
// $("div.new:eq(0)").slideUp()
$("div.new").hide();
$("div[name=arrow]").show();
$("a[name=new]").hide();
$("#news" + getID + "arrow").hide();
$("#news" + getID + "_info" ).slideDown();
$("#news" + getID ).html('<< Hide').slideDown();
}
});
});
</script>
The html code is below:
<div id="content">
<div class="news_year">
<a href="#" name="show" id="2012">
<div style="float:left;" name="year" id="news2012year">**2012** </div>
<div style="float:left;" name="arrow" id="news2012arrow">>></div>
</a>
</div>
<div class="new" id="news2012_info">
<div class="news">
<div class="news_left">News for 2012</div>
</div>
<div class="nhide" ><< Hide </div>
</div>
<div id="content">
<div class="news_year">
<a href="#" name="show" id="2011">
<div style="float:left;" name="year" id="news2012year">2012 </div>
<div style="float:left;" name="arrow" id="news2012arrow">>></div>
</a>
</div>
<div class="new" id="news2011_info">
<div class="news">
<div class="news_left">News for 2011</div>
</div>
<div class="nhide" ><< Hide </div>
</div>
Fiddle
if i am understanding your problem,
event.preventDefault(); not works with all browser so if you are using other browser like IE
then use event.returnValue = false; instead of that.so you can detect your browser using javascript as
var appname = window.navigator.appName;
This is what I'm currently using in my projects to "disable" an anchor tag
Disabling the anchor:
Remove href attribute
Change the opacity for added effect
<script>
$(document).ready(function(){
$("a").click(function () {
$(this).fadeTo("fast", .5).removeAttr("href");
});
});
</script>
Enabling the anchor:
$(document).ready(function(){
$("a").click(function () {
$(this).fadeIn("fast").attr("href", "http://whatever.com/wherever.html");
});
});
Original code can be found here
Add a class called 'shown' to your wrapper element when expanding your element and remove it when hiding it. Use .hasClass('shown') to ensure the inappropriate conditional is never executed.
Surround the code inside of the click function with an if statement checking to see if a variable is true or false. If it is false, it won't run the code, meaning the link is effectively inactive. Try this..
var isActive = true;
if (isActive) {
// Your code here
}
// The place where you want to de-activate the link
isActive = false;
You could also consider changing the link colour to a grey to signify that it is inactive.
Edit
Just realised that you want to have multiple links being disabled.. the code above will disable all of them. Try the code below (put the if around the code in the click function)
if(!$(this).hasClass("disabled")) {
// Your code here
}
// The place where you want to de-activate the link
$("#linkid").addClass("disabled");
// To re-enable a link
$("#linkid").removeClass("disabled");
// You can even toggle the link from disabled and non-disabled!
$("#linkid").toggleClass("disabled");
Then in your CSS you could have a declaration like this:
.disabled:link {
color:#999;
}