I am trying to hide and show an area based on whether a checkbox is checked. I've tried some options but either the area is visible all of the time or it is hidden all of the time.
JavaScript :
$(document).ready(function () {
var mgift = $('#chkbxMGift input[type=checkbox]');
MshowHide();
mgift.change(function () {
MshowHide();
});
});
function MshowHide() {
var mgift = $('#chkbxMGift input[type=checkbox]');
var shcompany = $('#shcompany');
if (mgift.checked) {
shcompany.show();
} else {
shcompany.hide();
}
}
HTML :
<li>
<div class="info">
<asp:CheckBox ID="chkbxMGift" runat="server" Text="A matching gift will be made" ClientIDMode="Static"/>
</div>
</li>
<li id="shcompany">
<div class="info">
<label for="txtCompanyName">Company Name</label>
<asp:TextBox runat="server" ID="txtCompanyName" CssClass="narrow" />
</div>
<div class="info">
<label for="txtCompanyPhone">Company Phone Number</label>
<asp:TextBox runat="server" ID="txtCompanyPhone" CssClass="narrow" />
</div>
</li>
How can I make this work correctly?
Try this code
$(document).ready(function () {
$('#chkbxMGift').click(function () {
var $this = $(this);
if ($this.is(':checked')) {
$('#shcompany').hide();
} else {
$('#shcompany').show();
}
});
});
Hope it solves your issue
Your selector is wrong.
var mgift = $('#chkbxMGift input[type=checkbox]');
This means you select the childnode input from parent #chkbxMGift.
I believe this is the selector you need:
var mgift = $('input#chkbxMGift[type=checkbox]');
And here are some improvements on your code:
$(document).ready(function () {
var mgift = $('input#chkbxMGift[type=checkbox]');
var shcompany = $('#shcompany');
// check for default status (when checked, show the shcompany)
if (mgift.attr('checked') !== undefined){
shcompany.show();
} else {
shcompany.hide();
}
// then simply toggle the shcompany on every change
mgift.change(function(){
shcompany.toggle();
});
});
jQuery's toggle is really useful and added in version 1.0, so you should be able to just go with that.
Here's a proof of concept in a jsFiddle, with only the bare minimum:
http://jsfiddle.net/Y39Bu/1/
This is stolen from this answer:
http://jsfiddle.net/MH8e4/4/
$('.wpbook_hidden').css({
'display': 'none'
});
alert($(':checkbox:checked').attr('id'))
$(':checkbox').change(function() {
var option = 'wpbook_option_' + $(this).attr('id');
if ($('.' + option).css('display') == 'none') {
$('.' + option).fadeIn();
}
else {
$('.' + option).fadeOut();
}
});
search for similar questions before you ask yours. Please give the original author the credit if this solves your problem
Have you tried changing the CSS directly? Such as
document.getElementById("shcompany").style.display="none";
or
document.getElementById("shcompany").style.visibility="hidden";
(or "visible")
Why use JQuery when a simple CSS property change can do the trick?
Just change the div's class or modify it's style:
If you want it to take up space even when hidden use visibility:hidden (respectively visibility:visible)
if you want it NOT to take space when hidden use css display:none (respectively display:block)
Related
I have developed a 3D model using JavaScript.
I need to show/hide a specific item in the model using a checkbox.
In the html file the checkbox has implemented.
<div id= "shower" class="row-shower" /div>
<input type="checkbox" id="show-shower" />
<label for="show-shower"> show/hide shower </label>
In the JS file, called the function:
var elem = document.getElementById('shower'),
showshower = document.getElementById("show-shower");
showshower.checked = true;
showshower.onchange = function() {
elem.style.display = this.checked ? 'cubicle' : 'none';
};
showshower.onchange();
CSS code:
.cubicle {
display:block;
}
.hideCubicle {
display:none;
}
The current issue I'm facing is when click the show/hide checkbox the whole model is gone. I just need the shower to be disappear. Not the whole model.
Any suggestion how to perform this?
var elem = document.getElementById('shower');
showshower = document.getElementById("show-shower");
showshower.onchange = function ()
{
elem.classList = this.checked ? 'cubicle' : 'hideCubicle';
};
http://jsfiddle.net/2kan1r80
You can change try code see
$(document).ready(function(){
$(".show-content").hide();
$('.check').change(function() {
if($(this).is(":checked")) {
$(".show-content").show();
}
else{
$(".show-content").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<label>Show/Hide</label>
<input type="checkbox" class="check">
<div class="show-content">
I have developed a 3D model using JavaScript.
I need to show/hide a specific item in the model using a checkbox.
In the html file the checkbox has implemented.
</div>
I found the solution.
I just add the following code to the JS file.
if(checked){
pSpec.cubicle.visible=false;
console.log("hide");
}
else
{
pSpec.cubicle.visible=true;
console.log("show");
}
You might wonder where the pSpec variable come from. That's the variable which I used to create partitions in the model. I did not share the whole code because it is lot of coding.
I have a click function setup whereby when you click on the .click div, it takes the data-hook attribute, and add it as a data-filter attribute to the .button div, this works fine, but after each click it is replacing the data-filter attribute with the new one, ideally I want to add a new value to the attribute with each click, separating each value with a comma.
Here's a jsFiddle demo: http://jsfiddle.net/neal_fletcher/pSZ2G/
HTML
<div class="button">THIS</div>
<div class="click" data-hook="one">CLICK</div>
<div class="click" data-hook="two">CLICK</div>
<div class="click" data-hook="three">CLICK</div>
<div class="click" data-hook="four">CLICK</div>
<div class="click" data-hook="five">CLICK</div>
jQuery:
$(".click").click(function () {
var thing = $(this).attr("data-hook");
$('.button').attr('data-filter', thing);
});
If this is at all possible? Any suggestions would be greatly appreciated!
You can store the previous value and can concatinate with new clicked value, something like.
$(".click").click(function () {
var thing = $(this).attr("data-hook");
var earData = $('.button').attr('data-filter');
if(typeof earData != 'undefined'){
$('.button').attr('data-filter', earData + ","+thing);
}else{
$('.button').attr('data-filter', thing);
}
});
DEMO
$(".click").click(function () {
var thing = $(this).attr("data-hook");
var prevValue = $('.button').attr('data-filter');
if(prevValue){
$('.button').attr('data-filter', prevValue +','+thing)
}
else{
$('.button').attr('data-filter', thing)
}
});
$(".click").click(function () {
var thing = $(this).attr("data-hook")||'';
var df = $('.button').attr('data-filter')||'';
$('.button').attr('data-filter', df+','+thing)
});
on a side note.. I hope you don't have any other elements with a .button class in it.. if you're looking to update a single target, you should reference by an id attrib instead.
$(function() {
$('#toggle3').click(function () {
$('.toggle').hide('1000');
$('.toggle').text('I would like to add a navigation menu here'); // <--
$('.toggle').slideToggle('1000');
return false;
});
});
I am wondering the best way to edit the above code snippet to be able to hold HTML / CSS as I plan on calling a custom menu within. I will be using this snippet multiple times and calling multiple menus to trigger with toggle.
If at all possible, try to avoid embedding html on javascript: you're likely to run into escaping issues and multiline strings, and the overall result usually isn't pretty.
You might want to store the HTML on the DOM itself:
<div>
<span class="toggle" data-toggle="foo">Toggle foo</span>
<span class="toggle" data-toggle="bar">Toggle bar</span>
</div>
<div id="navmenu-store">
<div class='navmenu' data-for-toggle="foo">
navmenu "foo"
</div>
<div class='navmenu' data-for-toggle="bar">
navmenu "bar"
</div>
</div>
On the CSS, hide the 'store':
#navmenu-store {
display: none;
}
And then, with javascript, add the navmenus when requested:
$(".toggle").each(function() {
var $toggle = $(this);
var toggleName = $toggle.data("toggle");
var $navmenu = $(".navmenu[data-for-toggle=" + toggleName + "]");
// Store the navmenu on the toggle for later access
$navmenu.remove();
$toggle.data("navmenu", $navmenu);
});
$(".toggle").on("click", function() {
var $toggle = $(this);
var $navmenu = $toggle.data("navmenu");
var isInTheDom = $.contains(document, $navmenu[0]);
if(isInTheDom) {
$navmenu.remove();
} else {
// Here I'm inserting the navmenu after the toggle;
// you can do whatever you want
$navmenu.insertAfter($toggle);
}
});
I've created a very simple jsbin as a proof of concept: http://jsbin.com/OwUWAlu/1/edit
I've seen various examples come close to what I am looking for, but none of it seems to describe it how I exactly want it. I am a beginner to jQuery, so explanations welcome.
I'm looking for this to toggle the innerHTML from - to +. Anyone know of a way to do this, efficiently?
jQuery/JavaScript
$(document).ready(function() {
$(".A1").click(function() {
$(".P1").toggle("slow");
$(".A1").html("+");
});
});
HTML
<div class="A1">-</div>
<h2 class="H1">Stuff</h2>
<div class="P1">
Stuffy, Stuffy, Stuffed, Stuffen', Stuffing, Good Luck Stuff
</div>
Thank you, anything relating to switching the inside text of an HTML element shall help. =)
How about adding a class that will let you know the expanded/collapsed status?
$(document).ready(function() {
$(".A1").click(function() {
var $this = $(this);
$(".P1").toggle("slow")
$this.toggleClass("expanded");
if ($this.hasClass("expanded")) {
$this.html("-");
} else {
$this.html("+");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="A1 expanded">-</div>
<h2 class="H1">Stuff</h2>
<div class="P1">
Stuffy, Stuffy, Stuffed, Stuffen', Stuffing, Good Luck Stuff
</div>
Example: http://jsfiddle.net/sGxx4/
$(document).ready(function() {
$(".A1").click(function() {
$(".P1").toggle("slow");
$(".A1").html(($(".A1").html() === "+" ? $(".A1").html("-") : $(".A1").html("+")));
});
});
A bit of explanation: I'm setting $("#A1").html() with the product of the tertiary operator, using it to check for the current value of #A1's text. If it's a +, I set the element's text to -, otherwise, I set it to +.
However, you said "efficiently." To this end, it's important to note that if you're going to use a selector twice or more in the same function, you should store the jQuery object that results from the selector you give in a variable, so you don't have to re-run the selector each time. Here's the code with that modification:
$(document).ready(function() {
$(".A1").click(function() {
var $A1 = $(".A1");
$(".P1").toggle("slow");
$A1.html(($A1.html() === "+" ? $A1.html("-") : $A1.html("+")));
});
});
There's no way to toggle content.
You could check if the $('.P1') is visible, then changing the +/- div according to that.
Something like :
$(document).ready(function() {
$(".A1").click(function() {
$(".P1").toggle("slow", function(){
if($(this).is(':visible'))
$(".A1").html("-")
else
$(".A1").html("+")
});
});
});
Using a callback function (the second argument of the .toggle() method) to do the check will guarantee that you're checking after the animation is complete.
JsFiddle : http://jsfiddle.net/cy8uX/
more shorter version
$(document).ready(function() {
$(".A1").click(function() {
var $self = $(this);
$(".P1").toggle("slow", function ( ) {
$self.html( $self.html() == "-" ? "+" : "-");
});
})
});
Here's a way that uses class names on a parent and CSS rules and doesn't have to change the HTML content and works off a container and classes so you could have multiple ones of these in the same page with only this one piece of code:
HTML:
<div class="container expanded">
<div class="A1">
<span class="minus">-</span>
<span class="plus">+</span>
</div>
<h2 class="H1">Stuff</h2>
<div class="P1">
Stuffy, Stuffy, Stuffed, Stuffen', Stuffing, Good Luck Stuff
</div>
</div>
CSS:
.expanded .plus {display:none;}
.collapsed .minus {display: none;}
Javascript:
$(document).ready(function() {
$(".A1").click(function() {
$(this).closest(".container")
.toggleClass("expanded collapsed")
.find(".P1").slideToggle("slow");
});
});
Working demo: http://jsfiddle.net/jfriend00/MSV4U/
I have a div that contains many spans and each of those spans contains a single href.
Basically it's a tag cloud. What I'd like to do is have a textbox that filters the tag cloud on KeyUp event.
Any ideas or is this possible?
Updated question: What would be the best way to reset the list to start the search over again?
Basically, what you want to do is something like this
$('#myTextbox').keyup(function() {
$('#divCloud > span').not('span:contains(' + $(this).val() + ')').hide();
});
This can probably be improved upon and made lighter but this at least gives the functionality of being able to hide multiple tags by seperating your input by commas. For example: entering this, that, something into the input will hide each of those spans.
Demo HTML:
<div id="tag_cloud">
<span>this</span>
<span>that</span>
<span>another</span>
<span>something</span>
<span>random</span>
</div>
<input type="text" id="filter" />
Demo jQuery:
function oc(a){
var o = {};
for(var i=0;i<a.length;i++){
o[a[i]]='';
}
return o;
}
$(function(){
$('#filter').keyup(function(){
var a = this.value.replace(/ /g,'').split(',');
$('#tag_cloud span').each(function(){
if($(this).text() in oc(a)){
$(this).hide();
}
else {
$(this).show();
}
})
})
})