jQuery: How to show a checkbox value on mouseover (without title attribute) - javascript

I am pretty new to jQuery and need some help with the following.
I have a HTML page with a large, dynamic number of checkboxes and would like to show a checkbox' value on mouseover without having to add a title attribute in the code for each of them.
Also, the value of a checkbox can be different to the text that is shown next to her (label).
The checkboxes are always in a div with the class '.divCheck' and the above applies to all checkboxes in these divs (all set up the same way).
So far I have the following which works as intended but seems to be very slow (at least in my test environment).
Is there a faster / better way to achieve this or can this be written different (either the jQuery or the HTML) to make it run faster ?
My HTML (simplified):
<div class="divCheck">
<input type="checkbox" name="language" class="checkSingle" id="language1" value="de - German" />
<label for="language1">de</label>
<input type="checkbox" name="language" class="checkSingle" id="language2" value="en - English" />
<label for="language2">en</label>
<input type="checkbox" name="language" class="checkSingle" id="language3" value="es - Spanish" />
<label for="language3">en</label>
<!-- ... -->
</div>
My jQuery:
$('div.divCheck label').on('mouseover', function(){
var checkID = $(this).attr('for');
var checkVal = $('#'+checkID).val();
$(this).prop('title', checkVal);
});
Many thanks for any help,
Mike

There is no need to use an event handler, you can set the title programatically like
$('div.divCheck label').attr('title', function(){
return $(this).prev('input').val()
});
$('div.divCheck label').attr('title', function() {
return $(this).prev('input').val()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="divCheck">
<input type="checkbox" name="language" class="checkSingle" id="language1" value="de - German" />
<label for="language1">de</label>
<input type="checkbox" name="language" class="checkSingle" id="language2" value="en - English" />
<label for="language2">en</label>
<input type="checkbox" name="language" class="checkSingle" id="language3" value="es - Spanish" />
<label for="language3">en</label>
</div>

You can set the title attribute of the checkbox to value. By this, you don't need any library/plugin.
$(':checkbox').prop('title', function() {
return $(this).val();
});
Demo: https://jsfiddle.net/zstupsbc/

a tooltip library such as qtip could help you

Related

How do i fix an <input type='radio'> where i select one and the others unselect?

Im relative new to Vue/Nuxt. I'm debugging a group of inputs type=radio that will not unselect when I select another option. I believe I have isolated the issue to this piece of code shown below. I'm trying to make sense of the code written by the other developer, especially the v-model= $store.state.currentModefiers. How do I go about fixing this bug that will not unselect the other options when I select a different
<div
v-for="(modifier, index) in getMenuModifiers(
groupModifiers._id
)"
:key="'MMi' + index"
>
<input
type="radio"
:id="index"
:ref="modifier._id"
:value="modifier"
#change="check($event)"
v-model="$store.state.currentModifiers"
/>
<label :for="modifier._id">{{ modifier.title }}</label>
</div>
It's my understanding that v-model serves as the model and links the various radio-button that share the same value for this attribute.
You should check if the value you've used is the same among those radio-buttons you want to be in the same group.
new Vue({
el: '#sauceChoice', data: {sauce: ''}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="sauceChoice" class="demo">
<h3>Salad Sauce</h3>
<input type="radio" v-model="sauce" value="Blue Cheese">Blue Cheese<br>
<input type="radio" v-model="sauce" value="Sweet Curry Mustard">Sweet Curry Mustard<br>
<input type="radio" v-model="sauce" value="Ranch">Ranch<br>
<br>
<p>value: {{sauce}}</p>
</div>
Source: Vue.js > Form input Bindings > Basic Usage > Radio

formidable toggle switch, how to click the label?

Currently, the below HTML code only switches when you click the input (ie. the center switch).
I need the switch to happen when you click the labels (ie. Yes or No).
How to make that happen?
<div>
<span class="frm_off_label frm_switch_opt">No</span>
<label class="frm_switch">
<input type="checkbox" name="2002" id="field_2002" value="Yes" checked="checked"
data-off="No" data-sectionid="2002" data-frmval="Yes" placeholder="Yes">
<span class="frm_slider"></span>
</label>
<span class="frm_on_label frm_switch_opt">Yes</span>
</div>
What are the ways to make the click of Yes or No, do the same thing as clicking the input?
You can achieve this by setting the checked status of input -
HTML
<div>
<span class="frm_off_label frm_switch_opt">No</span>
<label class="frm_switch">
<input type="checkbox" name="2002" id="field_2002" value="Yes" checked="checked"
data-off="No" data-sectionid="2002" data-frmval="Yes" placeholder="Yes">
<span class="frm_slider"></span>
</label>
<span class="frm_on_label frm_switch_opt">Yes</span>
</div>
JS (with jquery)
$('.frm_off_label').on('click', function(){
$(this).next('label').find("input")[0].checked = false;
});
$('.frm_on_label').on('click', function(){
$(this).prev('label').find("input")[0].checked = true;
});
CSS (optioanl)
.frm_switch_opt{
cursor:pointer;
}
You can see it in action on jsfiddle
https://jsfiddle.net/guruling/tegmps9b/27/
You can use label combine with for="element_id" like for="field_2002" attribute to achieve it.
The for attribute specifies which form element a label is bound to.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="field_2002" class="frm_off_label frm_switch_opt">No</label>
<input type="checkbox" name="2002" id="field_2002" value="Yes" checked="checked"
data-off="No" data-sectionid="2002" data-frmval="Yes" placeholder="Yes">
<label for="field_2002" class="frm_on_label frm_switch_opt">Yes</label>
</div>

How to show all divs that contain the similar combination of classes

I would like to show specific posts on a Wordpress page and hide all others. What posts are to be shown, should depend on checkboxes checked on top of the page. A checkbox corresponds with a class. When (for example) two checkboxes are checked (two classes selected in essence), only posts containing at least those two classes in their wrapping div (besides a possible third or fourth class they may have) should be shown (and all the other posts hidden) instantly on the page.
Does anyone of you know how to pull this off?
I already managed to get the tags assigned to a post in the CMS and transfer those tags to the wrapping div of that very post as its class names, so far so good.
How to write the last piece of needed code from scratch I find very difficult to do with the small amount of knowledge I have. I understand that first the desired classes should be gathered (selected through the checked checkboxes). Then a condition should be formed (class1 AND class3 AND class7 have been selected). Then, if that condition is true for a post (‘your wrapping div contains class1, class3 and class7’), only then the post should be shown. The post could also (for example) contain class2, that’s fine, the filter though is made up of the combination of class1, class3 and class7 being present within the wrapper div, as long as that’s true, it may be shown).
I’m sure this should be a stroll through the park for a lot of you, for me it’s still pretty hard to realise from scratch, so any help you guys could give me is highly appreciated! Thanks in advance!
Thanks for your response so far, guys, closest thing I found online so far is this:
$("#filters :checkbox").click(function() {
$("div").hide();
$("#filters :checkbox:checked").each(function() {
$("." + $(this).val()).show();
});
});
It comes from this setup: http://jsfiddle.net/6wYzw/41/ > here it shows a post when its wrapping div contains classX AND/OR classY, see its working example. I reckon the part where it says
$("." + $(this).val()).show();
... needs to be adjusted, so that right here all the checked checkboxes/classes so far are taken into account using AND, not OR. Is this the only line which actually has to be adjusted in order to make my desired setup work the way I want it too?
#Lisrael > all posts should be displayed at first, then the filter starts to kick in as soon as checkboxes are starting to be clicked just like you said indeed.
Found out how to do this! With the help of an example which came close already and lot's of 'sub'examples here and there. See the code.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h3 align="center">Boardgamefilter</h3>
<form name="gamesettings">
<input type="checkbox" name="gamesetting" value="1p">1p<br />
<input type="checkbox" name="gamesetting" value="2p">2p<br />
<input type="checkbox" name="gamesetting" value="3p">3p<br />
<input type="checkbox" name="gamesetting" value="4p">4p<br />
<input type="checkbox" name="gamesetting" value="5p">5p<br />
<input type="checkbox" name="gamesetting" value="6plus">6plus<br />
<br />
<input type="checkbox" name="gamesetting" value="15m">15m<br />
<input type="checkbox" name="gamesetting" value="30m">30m<br />
<input type="checkbox" name="gamesetting" value="45m">45m<br />
<input type="checkbox" name="gamesetting" value="60m">60m<br />
<input type="checkbox" name="gamesetting" value="90m">90m<br />
<input type="checkbox" name="gamesetting" value="120mplus">120mplus
<br />
<br />
<input type="button" name="Un_CheckAll" value="Reset"
onClick="UnCheckAll(document.gamesettings.gamesetting)">
</form>
<br />
<br />
<div id="boardgames">
<div class="1p 2p 15m 30m" style="display: block;">1p 2p 15m 30m</div>
<div class="1p 2p 30m 45m" style="display: block;">1p 2p 30m 45m</div>
<div class="2p 30m 60m" style="display: block;">2p 30m 60m</div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$('input[name=gamesetting]').change(function(){
var arr = []
$(":checkbox").each(function(){
if($(this).is(":checked")){
arr.push($(this).val())
}
})
var vals = arr.join(".")
var str = (".") + vals
$('#boardgames div').hide();
$('#boardgames div' + (str)).show();
})
function UnCheckAll(chk) {
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
$('#boardgames div').show();
}
</script>
</body>
</html>

check/uncheck bootstrap checkboxes with jQuery

I've made another post yesterday and had no success with it. I will try now to reorder de idea and to ask again since I can't find the solution for this (I'm a newbie and I'm trying really hard).
The thing is that I'm working with this template based on Bootstrap, and this is the code that I can see directly from one of the files and that I'm using on my project:
html snippet:
<div class="hide" id="states">
<div class="control-group">
<label class="control-label">Seleccione</label>
<div class="controls">
<label class="checkbox span4">
<input type="checkbox" value="all" id="allstates" name="all"/>Todas
</label>
<label class="checkbox span4">
<input class="states" type="checkbox" value="Caba" id="" name="st[]"/>Ciudad Autónoma de Buenos Aires
</label>
<label class="checkbox span4">
<input class="states" type="checkbox" value="Buenos Aires" id="" name="st[]"/> Buenos Aires
</label>
<label class="checkbox span4">
<input class="states" type="checkbox" value="Catamarca" id="" name="st[]"/> Catamarca
</label>
</div>
</div>
</div>
After lot of searching I have founded when inspecting it that my code looks like this (snippet):
<div class="controls">
<label class="checkbox span4">
<div class="checker" id="uniform-allstates">
<span>
<input type="checkbox" value="all" id="allstates" name="all">
</span>
</div>Todas
</label>
<label class="checkbox span4">
<div class="checker">
<span>
<input class="states" type="checkbox" value="Caba" id="" name="st[]">
</span>
</div> Ciudad Autónoma de Buenos Aires
</label>
<label class="checkbox span4">
<div class="checker">
<span>
<input class="states" type="checkbox" value="Buenos Aires" id="" name="st[]">
</span>
</div> Buenos Aires
</label>
<label class="checkbox span4">
<div class="checker">
<span>
<input class="states" type="checkbox" value="Catamarca" id="" name="st[]">
</span>
</div> Catamarca
</label>
</div>
As you can see, another div and another span are added (seriously don't know why or how) so I'm guessing now it's some DOM problem the one I'm having.
This is snippet of my js file:
$('#allstates').click(function() {
$('.checkbox').find('span').addClass('checked');
});
So this function selects ALL the checkboxes I have (even though the ones that I don't want because they belong to another category).
I want to know why the div and span are added and how to select only the checkboxes that I want.
For that I've been trying code like this but no success:
test 1
$('#allstates').click(function() {
if ($(this).is(':checked')) {
$('span input').attr('checked', true);
} else {
$('span input').attr('checked', false);
}
});
test2
$('#allstates').click(function() {
$(".states").prop("checked",$("#allstates").prop("checked"))
});
and so other ones that I erased.
NOTE: No js errors are being displayed when inspecting
Also it is important to add prop to true on selecting checkboxes:
$("#checkAll").click(function(){
var check = $(this).prop('checked');
if(check == true) {
$('.checker').find('span').addClass('checked');
$('.checkbox').prop('checked', true);
} else {
$('.checker').find('span').removeClass('checked');
$('.checkbox').prop('checked', false);
}
});
Since I mentioned that this function checked all the checkboxes I had (even though the ones that were not supposed to be checked), I've been trying to look for the solution:
$('#allstates').click(function() {
$('.checkbox').find('span').addClass('checked');
});
But this is not what I wanted. I mentioned also that I noticed that the code was "different" when inspecting it (in Chrome with F12). So as #thecbuilder said, it changes in order to add style.
I realized that since code is changing... I had to change the class (checkbox) from which I was starting the "span tag" search, so I change class checkbox to id states and this is the result:
$('#allstates').click(function() {
if($(this).attr("checked")){
$('#states').find('span').addClass('checked');
}else{
$('#states').find('span').removeClass('checked');
}
});
Now it finally works.
I think you are looking for code like:
$(document).ready(function()
{
$("#allstates").on("change", function()
{
var checked = $(this).prop("checked");
$(this).parents(".controls")
.first()
.find("input[type=checkbox]")
.prop("checked", checked);
});
});
JsFiddle Example
When the state of the checkbox with the id allstates changes, get the checkbox value, find the first parent html element with a class called controls and then only update all the checkboxes in that element with the value of checked.
Simple bootstrap checkbox
<label><input type="checkbox" value="">Option 1</label>
When debug in Chrome dev tool, label look like
https://tinker.press/images/bootstrap-checkbox-label-2017-01-17_084755.png
To toogle checkbox state just set true|false on lable.control.checked
label.control.checked = false; //uncheck
label.control.checked = true; //check
Video demo https://youtu.be/3qEMFd9bcd8

Using javascript and html to show images when checkboxes are checked

I'm trying to create a page that generates simple custom reports based on the checkboxes a user clicks. On the left side I will have a vertical column of checkboxes. For simplicity's sake, lets say I have two checkboxes labeled "Population" and "Employment".
When a user is interested in seeing employment data they check the "Employment" box and the image file of the data "employment.jpg" will be displayed to the right. If they then uncheck the box, the image will disappear. If they check both boxes, both images will be similarly displayed, one below the other in the order clicked.
I'm am loosely familiar with HTML, and new to Javascript. I've been trying to do this with if statements and document.write but can't keep my checkboxes on the page when the image is generated.
Here's my current code:
<html>
<head>
<script language="javascript" type="text/javascript">
function checker(that) {
if (that.checked) {
document.write("<br /><br /><img src='employment.png'>");
}
}
</script>
</head>
<body>
<form>
<input type="checkbox" name="Box" onclick="checker(this)"> Employment <br />
</form>
</body>
</html>
Here's a quick example to get you started. You can see it in action here.
function toggleVisibility(id) {
var el = document.getElementById(id);
if (el.style.visibility=="visible") {
el.style.visibility="hidden";
}
else {
el.style.visibility="visible";
}
}
<label for="chkemployment">Employment</label>
<input type="checkbox" id="chkemployment" onChange="toggleVisibility('imgemployment');" /><br/>
<label for="chkpopulation">Population</label>
<input type="checkbox" id="chkpopulation" onChange="toggleVisibility('imgpopulation');" />
<hr />
<img id="imgemployment" src="http://www.gravatar.com/avatar/c0d7be6d99264316574791c1e4ee4cc4?s=32&d=identicon&r=PG" style="visibility:hidden"/>
<img id="imgpopulation" src="http://www.gravatar.com/avatar/c0d7be6d99264316574791c1e4ee4cc4?s=32&d=identicon&r=PG" style="visibility:hidden" />
This is how the solution looks like in AngularJS:
<script src="http://docs-next.angularjs.org/angular-0.10.1.min.js"
ng:autobind></script>
<p>
<input type="checkbox" name="production" id="production" />
<label for="production">Production</label>
</p>
<p>
<input type="checkbox" name="employment" id="employment" />
<label for="employment">Employment</label>
</p>
<img src="http://i.i.com.com/cnwk.1d/i/bto/20071106/OLPC_photo_540x360.jpg"
ng:show="production" />
<img src="http://www.sunriseenterprisesinc.com/main/Portals/0/EmploymentSmall.jpg"
ng:show="employment" />
You can play with the example here: http://jsfiddle.net/psyho/nrdnx/
You can handle the change event of the checkboxes. Here's a full example (without images, though): http://jsfiddle.net/minitech/hsF9s/
Usually people use a framework such as jQuery. It allows you to abstract away from the nitty gritty details and cross-browser pains. Using that, you would do the task you describe like this:
http://jsfiddle.net/3vQJZ/3/
And here's the code from that working demo:
<input type="radio" name="selectPicture" value="http://i170.photobucket.com/albums/u277/AmandaisAwesomeQUACK/monkey.gif"/>
<input type="radio" name="selectPicture" value="http://freesmileyface.net/smiley/animals/monkey-crush2.gif" checked/>
<img id="image" src="http://freesmileyface.net/smiley/animals/monkey-crush2.gif" alt="image selected by radio buttons"/>
$(document).ready(function(){
$('input[name=selectPicture]').click(function(){
$('#image').attr('src', $('input[name=selectPicture]:checked').val());
});
});

Categories