The validation is needed to block the user to continue to the next page. They must have check one of the radio buttons etc.
<div class="">
<div class="radiho" style="display: block">
<input type="checkbox" name="speakenglish" value="true" id="speakenglish" class="yeseng">
<label for="speakenglish" class="radio-label">Yes please.</label>
</div>
<div class="rahdio" style="display: block">
<input type="checkbox" name="speakenglish" value="false" id="dontspeakenglish" class="noeng" checked>
<label for="dontspeakenglish" class="radio-label"> No thank you, not at this time.</label>
</div>
</div>
I have some JS that works with the .req class, but that just makes both boxes have to be checked which is wrong. The bottom one I tried to use but the page just skipped validation totally and went onto the next page.
thisObj.find('input[type=checkbox].req').each(function () {
if ($(this).prop("checked") == false) {
empty = true;
$(this).siblings('label').addClass('error');
}
});
thisObj.find('input[type=checkbox].yeseng' || 'input[type=checkbox].noeng').each(function () {
if ($(this).prop("checked") == false) {
empty = true;
$(this).siblings('label').addClass('error');
}
});
I think for the yeseng/noeng selector you want:
thisObj.find('input[type=checkbox].yeseng,input[type=checkbox].noeng')
or:
thisObj.find('input[type=checkbox].yeseng').add('input[type=checkbox].noeng')
Finally figured it out so you must check at least one box.
thisObj.find('input[type=radio].engdriver' ).each(function () {
if(!thisObj.find('input[type=radio].engdriver:checked').val()) {
empty = true;
$(this).siblings('label').addClass('error');
}
});
Related
I have a checkbox hidden div with text field. If checkbox is checked this text field will showed, but if checkbox unchecked text field not show. This Code :
<script type="text/javascript">
$(function () {
$("input[name='checkbox1']").click(function () {
if ($("#checkyes1").is(":checked")) {
$("#dvcheckbox1").show('lainnya');
} else {
$("#dvcheckbox1").hide('lainnya');
}
});
});
</script>
<div style="margin-left: 29%; margin-top: -2%;">
<label for="checkyes1">
<input type="checkbox" id="checkyes1" name="checkbox1" value="lainnya" />
Lainnya
</label>
<div id="dvcheckbox1" style="display: none;">
<?php echo $form->textField($model,'lainnya'); ?>
</div>
</div>
But, How this checkbox is checked if there something value on textfield? and text field shown. Because in form Update, value was show but checkbox is checked. I want checkbox is checked
For example Like this:
Click to show example
Instead use with change event with .toggle(boolean) method:
// on page load check of the div contains any text
$("#dvcheckbox1").toggle(function(){
var $v = $(this).find('input').val();
$("input[name='checkbox1']").prop('checked', $v.trim().length !== 0)
return $v.trim().length !== 0;
});
$("input[name='checkbox1']").change(function () {
$("#dvcheckbox1").toggle(this.checked);
});
There are a few things here:
1 - Better if you use document ready, because it waits until all the elements are loaded.
2 - You should hide the element$("#dvcheckbox1").hide('lainnya') when the page is loaded.
3 - A listener in the input:text is a good idea. It enables the checkbox if the textbox is empty.
Please, have a look at the next piece of code:
<div>
<label for="checkyes1">
<input type="checkbox" id="checkyes1" name="checkbox1" value="lainnya" />
Lainnya
</label>
<div id="dvcheckbox1">
<label for="myValuye"/>
<input type="text" id="myValue" name="myValue" value=""/>
</div>
</div>
<script>
$(document).ready(function() {
$("input[name='checkbox1']").click(function () {
// Always use some logs to check what line are you reaching.
console.log("Clicked checkbox!!!");
if ($("#checkyes1").is(":checked")) {
$("#dvcheckbox1").show('lainnya');
} else {
console.log("Value: " + $("#myValue").val());
$("#dvcheckbox1").hide("lainnya");
}
});
$("#myValue").bind("change paste keyup", function() {
if ($(this).val() == "") {
$("#checkyes1").removeAttr("disabled");
} else {
$("#checkyes1").attr("disabled", true);
}
});
// First of all we hide the #dvcheckbox1
$("#dvcheckbox1").hide('lainnya');
})
I have a really long form that I'm trying to create a clickable and tabbable(FocusOut of last input) accordion style effect which opens the next div while hiding the previous. Below is the html:
<form class="common">
<div class="hidesfeildset">
<feildset>
<legend>Section Title:</legend>
<label><input type="text"/></label>
<label><input type="text"/></label>
</fieldset>
</div>
<div class="hidesfeildset">
<feildset>
<legend>Section Title:</legend>
<label><input type="text"/></label>
<label><input type="text"/></label>
</fieldset>
</div>
<div class="hidesfeildset">
<feildset>
<legend>Section Title:</legend>
<label><input type="text"/></label>
<label><input type="text"/></label>
</fieldset>
</form>
And the js:
<script>
$(document).ajaxSuccess(function(){
$(".hidesfieldset").hide();
$("legend").bind("click","focusout",function () {
$(this).next(".hidesfieldset").toggle();
});
});
</script>
I cant get this to work for the life of me, does anyone see what I am doing wrong?
THanks in advance,
Mark
You have misspelled "fieldset" (not feildset) on every hidesfieldset class name as well as the opening fieldset tags. Furthermore, you haven't closed your final hidesfieldset div.
I won't ask your reasons of you choosing the html structure you did, but here is a working fiddle for you to look at and learn from.
http://jsfiddle.net/s4vcX/
// hide all labels (inputs) except for those in the first fieldset
$("fieldset label").hide();
$("fieldset:first label").show();
// show when legend is clicked while hiding rest
$("legend").bind("click", function () {
$("fieldset label").not($(this).nextAll("label")).hide();
$(this).nextAll("label").show();
});
//handle shift-tab on first input of each field set
$("fieldset").find("input:first").bind("keydown", function (e) {
if( e.shiftKey && e.which == 9 ) {
$(this).closest(".hidesfieldset").find("label").hide();
var previous = $(this).closest(".hidesfieldset").prev(".hidesfieldset");
if(previous.length==0)
previous = $(this).closest("form").find(".hidesfieldset:last");
previous.find("label").show();
previous.find("input").last().focus();
e.preventDefault();
}
});
//handle tab on last input of each field set
$("fieldset").find("input:last").bind("keydown", function (e) {
if( !e.shiftKey && e.which == 9 ) {
$(this).closest(".hidesfieldset").find("label").hide();
var next = $(this).closest(".hidesfieldset").next(".hidesfieldset");
if(next.length==0)
next = $(this).closest("form").find(".hidesfieldset:first");
next.find("label").show();
next.find("input").first().focus();
e.preventDefault();
}
});
I have two checkboxes and one of the checkboxes must be checked. I can see that it's right, no syntax errors. What should be made to my code to check if none of the checkboxes were checked?
HTML :
<input type="checkbox" value="aa" class="first" name="a"> Yes<br/>
<input type="checkbox" value="bb" class="second" name="b"> No <br/>
<button type="submit">Go!</button>
<p class="error"></p>
JavaScript:
$('button').on('click',function(){
if( $(".first:not(:checked)") && $(".second:not(:checked)") ){
$('.error').text('You must select atleast one!');
}else
$('.error').hide();
});
Example : http://jsfiddle.net/ptbTq/
Check this fiddle: http://jsfiddle.net/692Dx/
Checking code:
if($('input[type="checkbox"]:checked').length == 0) {
alert('none checked');
}
You are using selectors which do not return boolean values which is what you need when writing an if condition. Here's what you could do:
$('button').on('click',function() {
if(!$(".first").is(":checked") && !$(".second").is(":checked")) {
$('.error').text('You must select atleast one!').show();
} else {
$('.error').hide();
}
});
or if you prefer and think it could be more readable you could invert the condiciton:
$('button').on('click',function() {
if($(".first").is(":checked") || $(".second").is(":checked")) {
$('.error').hide();
} else {
$('.error').text('You must select atleast one!').show();
}
});
Also notice that you need to .show() the error message in the first case as you are hiding it in the second.
And here's a live demo.
Short:
$("input[type=checkbox]").is(":checked")
returns true if:
one of your checkboxes - from the selector ("input[type=checkbox]") - is checked.
else return false
and in your case:
$(".first, .second").is(":checked")
Do something at least one of your checkboxes is checked
Put the same class on both checkboxes and you can do something like
if ($(':checkbox.the_class:checked').length > 0) {
// at least one checkbox is checked
// ...
}
The best would be to put your checkboxes inside a div with an unique ID so you can verify all the checkboxes in there, so your code will work in all cases. Even when adding new checkboxes to the div later on.
<div id="cb">
<input type="checkbox" value="aa" class="first" name="a" /> Yes<br/>
<input type="checkbox" value="bb" class="second" name="b" /> No <br/>
<button type="submit">Go!</button>
<p class="error"></p>
</div>
Your JQuery:
$('button').click(function() {
var checked_one = $('div#cb input[type="checkbox"]').is(':checked');
if (!checked_one )
$('.error').text('You must select atleast one!');
else
$('.error').hide();
});
Live demo can be seen here: http://jsfiddle.net/ptbTq/15/
I have 2 radio buttons no one of them checked by default and I want if any one of them checked a Div appear according to what radio button was checked.
( Divs have different content )
and if the selection changed the one which appeared now disappear and the other appear.
and when one of them appear there are another 2 radio to do the same thing for another one div ( one to show and one to hide )
Here what I tried to do
JavaScript
function haitham()
{
if(document.getElementById('s').checked == true)
{
document.getElementById('StudentData').style.display = "block";
document.getElementById('GraduateData').style.display = "none";
}
else if(document.getElementById('g').checked == true)
{
document.getElementById('GraduateData').style.display = "block";
document.getElementById('StudentData').style.display = "none";
}
}
function info()
{
if(document.getElementById('y').checked == true)
{
document.getElementById('MoreInfo').style.display = "block";
}
else if(document.getElementById('n').checked == true)
{
document.getElementById('MoreInfo').style.display = "none";
}
}
HTML
<input class="margin2" id="s" type="radio" name="kind" value="student" onchange="haitham()"
required="required" />Student
<input class="margin2" id="g" type="radio" name="kind" value="graduate" onchange="haitham()"
required="required" />Graduate
<div id="StudentData">
content 1
<input class="margin2" id="y" type="radio" name="info" value="yes" onchange="info()"
required="required" />Student
<input class="margin2" id="n" type="radio" name="info" value="no" onchange="info()"
required="required" />Graduate
</div>
<div id="GraduateData">
content 2
</div>
<div id="MoreInfo">
content 3
</div>
the first work good but the other 2 radio did not work although it should be the same
Thank you ...
Your problem wasn't a javascript or html one, it was actually a CSS issue. Your code was fine, aside from the fact that the values for display are "none" and "block" not "" and "hidden". I modified your code and updated the fiddle.
Here's the link:
http://jsfiddle.net/8JpSQ/4/
Just add a clicked event to the radio buttons, and through a Javascript function change the attribute of the respective DIV to hidden when required. To show it instead, remove the attribute 'hidden'. Also, we'd probably be able to help more if you can post some code showing what you tried/what went wrong. But what I suggested should be the general approach to make what you want happen.
I have no idea what your HTML is, so here's what I have:
$('input[type="checkbox"]').click(function() {
$('.divWrapper > div').eq($(this).index()).fadeOut().siblings().fadeIn();
});
I'm assuming this is your structure:
<form>
<checkbox>
<checkbox>
...
</form>
<div class="divWrapper">
<div>
<div>
...
</div>
I copied a pice of jquery code from a tutorial for working with radio buttons. It is not working for me but from the comments on the tutorial it seems to work for other people. I get this "missing ) after argument list" } else {
So here is the relevant portion of my code. I have two radio buttons above two forms. I want to show the form with the checked radio button and hide the other form.
<script type="text/javascript">
$(document).ready(function() {
$('#existing-login').show();
$('#new-customer').hide();
});
$("input[#name='customer']").change(function(){
if ($("input[#name='customer']:checked").val() == "new")
$("#new-customer").show();
$("#existing-login").hide();
} else {
$("#existing-login").show();
$("#new-customer").hide();
}
});
</script>
<input type="radio" name="customer" value="existing" checked="checked"/>
<input type="radio" name="customer" value="new"/>
<div id="existing-login">
<!-- form for existing customer login -->
</div>
<div id="new-customer">
<!-- form for new customers -->
</div>
Missing { after your if statement:
if ($("input[#name='customer']:checked").val() == "new") {
$("#new-customer").show();
$("#existing-login").hide();
} else {
$("#existing-login").show();
$("#new-customer").hide();
}
Also the # syntax for selecting attributes isn't supported anymore in current versions of jQuery, so remove that too:
if ($("input[name='customer']:checked").val() == "new") {