If check box is check display div but hide previous open div - javascript

<input type="checkbox" data-related-item="title1">
<span class="caption">Title</span>
<div class="hidden">
<h2>Title</h2>
<input type="text" id="title1">
<span class="caption">Lorem</span>
</div>
<hr>
<input type="checkbox" data-related-item="title2" checked>
<span class="caption">Title</span>
<div class="hidden">
<h2>Title</h2>
<input type="text" id="title2">
<span class="caption">Lorem</span>
</div>
Javascript
function evaluate(){
var item = $(this);
var relatedItem = $("#" + item.attr("data-related-item")).parent();
if(item.is(":checked")){
relatedItem.fadeIn();
}else{
relatedItem.fadeOut();
}
}
$('input[type="checkbox"]').click(evaluate).each(evaluate);
This was about this post: if check box is checked display div
I would like to ask how do I use this code but every time you click another checkbox it hides the previous div and opens a new one. Just showing one div/content at a time instead of showing both open. Thanks.
DEMO: http://jsfiddle.net/zgrRd/5/

You must use change instead of click function and inside evaluate function, add the following line at the start
$('input[type="checkbox"]').not($(this)).prop('checked',false).trigger('change');
Watch here fiddle

If I understand you correctly, you want only one <div> open at a time, which should correlate to a checkbox. I interpret this, that you also only checkbox checked at a time, so all other checkboxes wold need to be unchecked.
Solution 1: Use radio buttons
Solution 2: Uncheck all checkboxes and re-check the current one.
The solution for showing the <div>s is based on solution 2. Just hide all <div>s first and then show your related <div>. Give all your <div>s a certain class (e.g. class="toggling") or name to be used as a selector for closing all.
$('input[type="checkbox"]').on("click", function() {
var item = $(this);
var relatedItem = $("#" + item.attr("data-related-item")).parent();
$('.toggling').fadeOut(); //hide all
relatedItem.FadeIn(); //show the current one
});

Related

Switch between divs using radio buttons

I am trying to show-hide div using jquery on click of radio buttons. It might be a weird question to ask but my brain is not digging more and i know that is easy task to do.
Below is HTML
<input type="radio" value="Active Now" class="tabActive" id="active-radio1"
/>Participations
<input type="radio" value="Not Active Now" class="tabNotActive"
id="active-radio2" />
Droppers
<div id="tabActive" class="tab-content">
</div>
<div id="tabNotActive" class="tab-content hide">
</div>
Below is JS
$("input:radio").off().on('click',function()){
var value = $(this).attr("class");
$("#"+value).show();
// I also tried
$("#"+value).toggleClass('hide'); /*Not right way, i know :)*/
$("#"+value+" .tab-content").toggleClass('hide')
});
I am not able to switch between divs due to hide class, but nothing worked
Note: The hide class is being added by framework and i can not modify it.
So, i need a perfect way to show hide these divs.
Try this.
$('input[type=radio]').on('click',function()) {
var id = $(this).attr('class'); // this is very prone to problems
$('.tab-content').addClass('hide')
$('#' + id).removeClass('hide');
});
You can try this one:
$(function() {
$("[name=toggler]").click(function(){
$('.toHide').hide();
$("#blk-"+$(this).val()).show('slow');
});
});
DEMO FIDDLE

Unexpected response for checkbox jQuery

I have horizontal jQuery checkbox. It should display some text when it is clicked and remove the text when it is clicked again and unchecked. However, when i first load the page and click on the box nothing happens. Then when i click it again to uncheck the text appears. It seems the opposite behaviour of what i expect is going on. Here is the code:
(I can solve this problem by simply inverting the boolean sign but i want to understand why this is happening).
<form>
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Select your type of Restaurant:</legend>
<input type="checkbox" name="checkbox-h-2a" id="checkbox-h-2a">
<label for="checkbox-h-2a" onclick="onfilter()">Vegetarian</label>
</fieldset>
</form>
<script>
function onfilter(){
if ($("#checkbox-h-2a").prop('checked')){
document.getElementById("hehe").innerHTML = "Yo there";
}
if (!($("#checkbox-h-2a").prop('checked'))){
document.getElementById("hehe").innerHTML = "";
}
}
</script>
You're already loading jQuery , so just use jQuery for everything - it is much easier , works better, really the only downside to jQUery is having to load it - and you're already doing that. So I would suggest using something like this:
$(function(){
$(document).on('click', '#checkbox-h-2a', function(){
if ( $(this).is(':checked') ) {
// Do stuff
}
else{
//Do stuff
}
});
});
Also, I hope you are actually closing your input element in your HTML , and that this is just a typo in your question
<input type="checkbox" name="checkbox-h-2a" id="checkbox-h-2a"
try:
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>Select your type of Restaurant:</legend>
<label for="checkbox-h-2a" >Vegetarian
<input type="checkbox" name="checkbox-h-2a" id="checkbox-h-2a" />
</label>
</fieldset>
see how the label goes around the checkbox? also you can get rid on the inline function in HTML with the jQuery I provided
EDIT:
2 problems - one you selectd jQuery 1.6 , to you .on() you need a newer version , if you must use old jQuery let me know ,
the other problem is that all jQuery code must be wrapped in
$(document).ready(function(){
/// code here
});
or for short:
$(function(){
// code here
});
The problem is at the time of clicking on the label, the checkbox's checked has not been changed, so you have to toggle the logic (although it looks weird) or attach the handler to the onchange event of the checkbox input instead:
<!-- add onchange event handler -->
<input type="checkbox" name="checkbox-h-2a" id="checkbox-h-2a"
onchange="onfilter()"/>
<!-- and remove the click handler -->
<label for="checkbox-h-2a">Vegetarian</label>
Demo.
It involves how a label works, when clicking on the label, it looks for the attached input element via the for attribute and trying to change the appropriate property (checked for checkbox, radio, ...) or focusing the element (for textbox fields). So at the clicking time, it processes/calls your handler first. Hence the problem.
Note that this answer just fixes the issue, not trying to improve your code.

Issues while reading value of input field in popover using jquery

I have a part of HTML like this which is generated dynamically. all tha value in popover are also generated dynamically. for now I have shown only one <li> value with the id = "loc" which is also given dynamically. I want to read the value of input field with id = "loc". whatever I have tried i can mention below:
in the first line of html you can see id = "close" . When a user will click on a icon with id = "close" I need to reed the value of li in the popover with the id = "loc".
I have tried
$('#data').on('click','.icon-remove',function(){
//var ptr=$(this).parents("tr.task");
//console.log($('i#close').find('p').css('background-color', 'red'));
//console.log(ptr.find(("#loc").val()));
//console.log($("#loc").val());
var $pel = $('.popover').find('#details')
console.log($pel.find('li#loc').val()); // through this I'm getting undefined
console.log($('.popover').find('#loc').val()); // this is giving me undefined
$('.popover').remove();
});
<i id=" close" class="icon-remove title-inner-pop closePop"></i>
<div class="popover-content">
<div>
<div class="more-option">
<span class="labels">1Yr Analytics</span>
<p>12 calls 15hours</p>
<a hrf="#">More...</a>
</div>
<div id="details">
<li>
<span class="labels">Location -</span>
<p>
<input id="loc" type="text" value="d">
</p>
</li>
I'll be really happy If some one can suggest me something to read the value in the input field with id = "loc" when someone click on icon inside popover with id="close".
thanks in advance.
Your over complicating this way to much. You have id's on your elements there is ZERO reason to use the find(). find() is for using advanced queries for dynamic content.
You have very static content simply use the below and that should work.
$("#close").click(function(){
var input = $("#loc").val();
})

Using javascript onClick to style currently selected radio button

I am trying to add the class "current" to whatever radio option is currently selected. I can use
onClick="document.getElementById('volunteering').class += 'current';"
However, this will indeed add the class, but it sticks when you select another radio button (because it's on each button). Any thoughts on how to implement it across all radio buttons?
Here is the code I am using:
<form>
<ul>
<input type="radio" name="category" id="volunteering" value="volunteering"><li class="conversation">
<a href="#" onClick="document.getElementById('volunteering').checked = true;" >Volunteering</a>
</li>
<input type="radio" name="category" value="cityproblems" id="cityproblems"><li class="conversation">
<a href="#" onClick="document.getElementById('cityproblems').checked = true;" >City Problems</a>
</li>
<input type="radio" name="category" value="safety" id="safety"><li class="conversation">
Safety
</li>
</form>
You need to first remove that class from all radio buttons, then add it back to the clicked one. Define a function that does this by receiving the id as a parameter, and bind that to the onclick.
function selectRadioClass(radioId) {
// Note - there are more clever ways, like arrays and loops
// for this, but if it's only 3, this is fine
document.getElementById('volunteering').className = '';
document.getElementById('cityproblems').className = '';
document.getElementById('safety').className = '';
// Then add it back to the node passed in by id
document.getElementById(radioId).className = 'current';
// And check the button
document.getElementById(radioId).checked = true;
}
Then bind each radio button's onclick using:
Safety
There are 2 solutions:
Use CSS3 :checked selector
form input:checked {
/* your style rules here. */
/* No more scripting needed. */
}
http://www.quirksmode.org/css/enabled.html
Use A global function for this purpose.
// check Michael's solution.
Use CSS pseudo selectors and forget the JavaScript. There is one for element:checked.
http://css-tricks.com/pseudo-class-selectors/

Using .change() function to auto-submit form on checkbox change?

This is a bit of a long question so please bear with me guys.
I needed to make a form submit automatically when a checkbox was ticked. So far I have the code below and it works perfectly. The form must submit when the check box is either checked or unchecked. There is some PHP that reads a database entry and shows the appropriate status (checked or unchecked) on load.
<form method="post" id="edituser" class="user-forms" action="--some php here--">
<input class="lesson" value="l101" name="flesson" type="checkbox" />
</form>
<script>
$('.lesson').change(function() {
$('.user-forms').submit();
});
</script>
However, when I introduce a fancy checkbox script which turns checkboxes into sliders it no longer works. The checkbox jQuery script is below:
<script src="'.get_bloginfo('stylesheet_directory').'/jquery/checkboxes.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$("input[type=checkbox]").tzCheckbox({labels:["Enable","Disable"]});
});
</script>
The contents of the checkboxes.js called to above is as follows:
(function($){
$.fn.tzCheckbox = function(options){
// Default On / Off labels:
options = $.extend({
labels : ['ON','OFF']
},options);
return this.each(function(){
var originalCheckBox = $(this),
labels = [];
// Checking for the data-on / data-off HTML5 data attributes:
if(originalCheckBox.data('on')){
labels[0] = originalCheckBox.data('on');
labels[1] = originalCheckBox.data('off');
}
else labels = options.labels;
// Creating the new checkbox markup:
var checkBox = $('<span>',{
className : 'tzCheckBox '+(this.checked?'checked':''),
html: '<span class="tzCBContent">'+labels[this.checked?0:1]+
'</span><span class="tzCBPart"></span>'
});
// Inserting the new checkbox, and hiding the original:
checkBox.insertAfter(originalCheckBox.hide());
checkBox.click(function(){
checkBox.toggleClass('checked');
var isChecked = checkBox.hasClass('checked');
// Synchronizing the original checkbox:
originalCheckBox.attr('checked',isChecked);
checkBox.find('.tzCBContent').html(labels[isChecked?0:1]);
});
// Listening for changes on the original and affecting the new one:
originalCheckBox.bind('change',function(){
checkBox.click();
});
});
};
})(jQuery);
There is also some CSS that accompanies this script but I am leaving it out as it is not important.
Finally, this is what the jQuery script does to the checkbox:
<input id="on_off_on" class="lesson" value="lesson11-1" name="forexadvanced[]" type="checkbox" style="display: none; ">
<span classname="tzCheckBox checked" class=""><span class="tzCBContent">Disable</span><span class="tzCBPart"></span></span>
When the checkboxes are changed into sliders the .change() function no longer detects the change in the checkboxes status.
How can I make the .change() function work or is their an alternative function I can use?
This plugin changes your checkboxes to span elements and hides the actual checkboxes themselves. Thus, when you click on them, nothing happens. Since span elements don't have onchange events, you can't bind change events to these.
However, span elements do have click events, meaning that you could instead bind a click event to the generated spans, using Firebug or Chrome Debugger to locate the correct element to bind to.
Your click-handler can then take the same action your change event would normally take if the plugin weren't being used.
Here is an example:
HTML (Source):
<!-- This is a checkbox BEFORE running the code that transforms the checkboxes
into sliders -->
<li>
<label for="pelda1">OpciĆ³ 1:</label>
<input class="pelda" type="checkbox" id="pelda1" name="pelda1" />
</li>
HTML (Generated From Chrome Debugger):
NOTE: This is the generated HTML after running the JavaScript that converts checkboxes to sliders! You must bind your click event AFTER this code is generated.
<li>
<label for="pelda1">Option 1:</label>
<!-- The hidden checkbox -->
<input class="pelda" type="checkbox" id="pelda1" name="pelda1" style="display: none; " />
<!-- the "checked" class on the span gets changed when you toggle the slider
if it's there, then it's checked. This is what you're users are actually
changing.
-->
<span class="tzCheckBox checked">
<span class="tzCBContent">active</span>
<span class="tzCBPart"></span>
</span>
</li>
JavaScript:
NOTE: This must be bound AFTER converting the checkboxes to sliders. If you try it before, the HTML won't yet exist in the DOM!
$('.tzCheckBox').click(function() {
// alert the value of the hidden checkbox
alert( $('#pelda1').attr("checked") );
// submit your form here
});
Listen for change like this:
$('.lesson').bind("tzCheckboxChange",function() {
$('.user-forms').submit();
});
Modify the plugin by adding the line:
$(originalCheckBox).trigger("tzCheckboxChange");
after
checkBox.find('.tzCBContent').html(labels[isChecked?0:1]);
This way, anytime you use this plugin, you can listen for tzCheckboxChange instead of just change. I don't really know what's going on with the plugin, but seems kinda funky for it to be listening for a change event when it would only be fired through trigger (unless it doesn't hide the original checkbox).

Categories

Resources