I have a series of radio buttons, which onClick will either reveal or hide a Div:
Reveal the Div:
<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" id="con4" />
Hide the Div:
<input type="radio" name="con[4]" value="0" onclick="toggleLayer4(!this.checked);" checked="checked" id="con4" />
JavaScript:
function toggleLayer4(val)
{
if(val == '1' || val === true)
{
document.getElementById('con4').checked = true;
document.getElementById('con4PSTN').style.display = 'block';
}
else if(val == '0' || val === false)
{
document.getElementById('con4').checked = false;
document.getElementById('con4PSTN').style.display = 'none';
}
}
Now the problem, when the pag is recalled, I can get the radio button checked like this:
<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(this.checked);" <? if ($conn_count[3] == 1){echo "checked=\"checked\"";}?> id="con4" />
But I need a away of calling the JavaScript function to reveal the div if the radio button is checked, I have tried to echo toggleLayer4(this.checked); within the PHP if statement inside tags, however this just seems to reurn the text in the html??
There must be a way, not really versed in JS.
Cheers,
B.
Here is a plain unobtrusive javascript for you which will save you some work - please notice there are no event handlers on the radios anymore.
I gave them unique IDs which is mandatory.
If you ever need to use jQuery for other stuff, this script can be a little more elegant.
I assume
<input type="radio" name="con[1]" value="1" id="con1_1" />
<input type="radio" name="con[1]" value="0" id="con1_0" />
<input type="radio" name="con[2]" value="1" id="con2_1" />
<input type="radio" name="con[2]" value="0" id="con2_0" />
<input type="radio" name="con[3]" value="1" id="con3_1" />
<input type="radio" name="con[3]" value="0" id="con3_0" />
<input type="radio" name="con[4]" value="1" id="con4_1" />
<input type="radio" name="con[4]" value="0" id="con4_0" />
and matching object to have conxPSTN where x is the number in the con[x]
window.onload=function() {
var conLength = <?php echo count($con); ?>;
for (var i=1;i<=conLength;i++) {
var cons = document.getElementsByName("con["+i+"]");
for (var j=0;j<cons.length;j++) {
cons[j].onclick=function() {
var id = this.id.split("_")[0];
document.getElementById(id+"PSTN").style.display = (this.value==1)?"block":"none"
}
if (cons[j].checked) cons[j].click();
}
}
}
<input type="radio" name="con[4]" value="0" onclick="toggleLayer4(0);" checked="checked" id="con4" />
<input type="radio" name="con[4]" value="1" onclick="toggleLayer4(1);" checked="checked" id="con4" />
function toggleLayer4(val)
{
if(val){
document.getElementById('con4PSTN').style.display = 'block';}
else{
document.getElementById('con4PSTN').style.display = 'none';
}
}
If you are echoing from php then you should do like
echo "<script type='text/javascript'>toggleLayer4(this.checked);</script>";
But you cant use this in there. You need to get the value of the radio button manually n pass it
EDIT
This is the case when you want to call the function explicitly in some part of the code and when the php code is not with the <script> tags.
Related
How to get value from checkbox or radio button immediately after clicking on it and write it in textfield without using reset or submit button?
<input type="checkbox" name="age" value="21-29">21-29 <input type="text" name="yourAge" value="">
You can do like this with jQuery click function More Detail Here
<input type="checkbox" name="age" value="21-29" id="age">21-29
<input type="text" name="yourAge" value="" id="yourAge">
JQuery
$("#age").click(function () {
$("#yourAge").val($("#age").val());
});
Fiddle
#Shehary is on point but there is always room for more.
JS
<script>
var changeInput = function (val){
var input = document.getElementById("age");
input.value = val;
}
</script>
HTML
<input type="checkbox" name="age" value="21-29" onclick='changeInput(this.value);' >21-29
<input type="text" name="yourAge" value="" id="age">
Pen
I know this is old, and my code may not be great, but I like to use this.
<?php
$get= "?checked";
$checked= "";
if(isset($_GET['checked'])){
$get= "";
$checked= "checked";
}
?>
<a href="waiting_in_db.php<?=$get?>">
<input type="checkbox" <?=$checked?> >
</a>
But I prefer using CSS and links without the checkbox.
I am new to HTML, I have a list of checkboxes on a form in an HTML page.
Each checkbox on each line represents a different category "I" "D" "C" and "S".
Part of my code is as follows:
<form>
1.<input type="checkbox" name="Personality_1.1" value="I"/>Animated  
<input type="checkbox" name="Personality_1.2" value="D" />Adventurous  
<input type="checkbox" name="Personality_1.3" value="C" />Analytical  
<input type="checkbox" name="Personality_1.4" value="S" />Adaptable<br /><br />
2.<input type="checkbox" name="Personality_2.1" value="I"/>Playful  
<input type="checkbox" name="Personality_2.2" value="D" />Persuasive  
<input type="checkbox" name="Personality_2.3" value="C" />Persistent  
<input type="checkbox" name="Personality_2.4" value="S" />Peaceful<br /><br />
3.<input type="checkbox" name="Personality_3.1" value="I"/>Sociable  
<input type="checkbox" name="Personality_3.2" value="D" />Strong Willed  
<input type="checkbox" name="Personality_3.3" value="C" />Self-sacraficing  
<input type="checkbox" name="Personality_3.4" value="S" />Submissive<br /><br />
I need to find out how many value "I" checkboxes have been checked, how many value "D" checkboxes have been checked, and so on, and then display the total of each category when the form is submitted.
Such a: "Five D's have been checked" "Three C's have been checked"
Is there a way I can do this with Javascript or PHP? If so can anyone help direct me to figure out how to do so?
Well, with PHP, assuming your submitting the form with POST:
$counts = array_count_values($_POST);
And you'll get an associative array with the values as keys and counts as values. So if for example 3 D's have been checked, $counts['D'] will hold "3".
As an example, you can use something like this:
window.onload = function () {
document.getElementById("btn1").onclick = function () {
var allChk = document.getElementsByTagName("input"),
counts = {},
i, j, cur, val;
for (i = 0, j = allChk.length; i < j; i++) {
cur = allChk[i];
if (cur.type === "checkbox") {
if (!(cur.value in counts)) {
counts[cur.value] = 0;
}
if (cur.checked) {
counts[cur.value]++;
}
}
}
for (val in counts) {
console.log("There are " + counts[val] + " " + val + "'s checked");
}
};
};
DEMO: http://jsfiddle.net/Dwjez/1/
Click the button, after checking some checkboxes, and look at your console to see the results. It just finds all checkboxes, and stores the number of checked ones, per value, in an object literal...then the final loop is there just to print the results in the console.
This was just a simple example with event handling, but I'd suggest looking at addEventListener vs onclick to see another way to handle events (with addEventListener).
jquery-:
var numberOfCheckboxesSelected = $('input[type=checkbox]:checked').length;
javascript--:
var checkboxLength = document.forms["formName"].elements["checkbox[]"].length;
var checkboxes = document.forms["formName"].elements["checkbox[]"];
for(var i = 0; i < checkboxLength; ++i) {
if(checkboxes[i].checked) {
// do stuff
}
}
how about...
var getCount = function(type) {
return document.querySelectorAll('input[value='+type+']:checked').length;
}
alert(getCount('A') + "As have been selected");
and it looks like you would be better off using a radio group instead of checkboxes. From looking at your html, do you want the user to be able to select more than one item in each section?
Here is the code you want. Try it and let me know.
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<FORM NAME="f1" action="next_page.php" method="post">
<input type="checkbox" name="chkGuar[]" value="mike"> Mike<br />
<input type="checkbox" name="chkGuar[]" value="joy"> Joy<br />
<input type="checkbox" name="chkGuar[]" value="harry"> harry<br />
<input type="checkbox" name="chkGuar[]" value="watson"> watson<br />
<input type="checkbox" name="chkGuar[]" value="george"> george<br />
<input type="checkbox" name="chkGuar[]" value="peter"> Peter<br />
<input type="submit" name="chksbmt" value="Send" />
<!-- <div id="myrow" style="visibility:hidden">
<input type = text name ='txtGRTNo' tabindex = 19 size="20">
</div>
<div width="338" align="left" colspan="3" height="12"></div> !-->
</FORM>
</BODY>
</HTML>
next_page.php
<?php
if(isset($_POST['chksbmt'])){
$counts = count($_POST['chkGuar']);
echo "this is the next page. you checked $counts checkbox <br /><br />";
for($i=1;$i<=$counts;$i++){
echo "<input type='text' style='border:1px solid #000;' value='your text box here' /><br/><br/>";
}
}
You should write your form code like this:
<form>
1.<input type="checkbox" name="chkI[]" value="I1"/>Animated
<input type="checkbox" name="chkD[]" value="D1" />Adventurous
<input type="checkbox" name="chkC[]" value="C1" />Analytical
<input type="checkbox" name="chkS[]" value="S1" />Adaptable
2.<input type="checkbox" name="chkI[]" value="I2"/>Playful
<input type="checkbox" name="chkD[]" value="D2" />Persuasive
<input type="checkbox" name="chkC[]" value="C2" />Persistent
<input type="checkbox" name="chkS[]" value="S2" />Peaceful
3.<input type="checkbox" name="chkI[]" value="I3"/>Sociable
<input type="checkbox" name="chkD[]" value="D3" />Strong Willed
<input type="checkbox" name="chkC[]" value="C3" />Self-sacraficing
<input type="checkbox" name="chkS[]" value="S3" />Submissive
</form>
Look at the "name" and "value" attributes. I made I change to the values of them.
You say:
I need to find out how many value "I" checkboxes have been checked, how many value "D" checkboxes have been checked, and so on, and then
display the total of each category when the form is submitted.
If you make a submit...
<?php
if(!empty($_GET['chkD'])) {
$counterChkD = 0;
foreach ($_GET['chkD'] as $chkD){
echo $chkD."\n";
//echoes the value set in the HTML form for each checked checkbox associated with the "D" value
//so, if I were to check "Adventurous", "Persuasive", and "Strong Willed" it would echo value D1, value D2, value D3.
$counterChkD++;
}
echo "# of 'D-CheckBoxes' checked: ".$counterChkD."\n";
}
?>
I have a web app (PHP) and I have to make this change. I am more of a database, scripting guy, please bear with me on this one!
I have 8 check boxes (think numbered 1~8) in a form. I have to implement a condition where in :
If one of the first 4 checkboxes are checked (only one checkbox can be checked in the first 4),
Then the next 4 checkboxes should be disabled
Else the next 4 checkboxes should be enabled.
My solution :
Make the first 4 checkboxes radiobuttons to confirm to the only one
checkbox can be selected condition.
Disable/Enable the next 4 checkboxes based on the above action. So,
if the radiobutton is not selected, then the next 4 checkboxes should
be available for selection.
I have to actually disable the checkboxes rather than hide using jQuery, so the checkboxes should be solidgray (uncheckable) when disabled.
Sample code (stripped off some formatting mess for others looking for a similar solution) :
<div>
<input type="checkbox" name="check1" value="1" id="check1" <?php if (!empty($rows['check1'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check2" value="1" id="check2" <?php if (!empty($rows['check2'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check3" value="1" id="check3" <?php if (!empty($rows['check3'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check4" value="1" id="check4" <?php if (!empty($rows['check4'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check5" value="1" id="check5" <?php if (!empty($rows['check5'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check6" value="1" id="check6" <?php if (!empty($rows['check6'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check7" value="1" id="check7" <?php if (!empty($rows['check7'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check8" value="1" id="check8" <?php if (!empty($rows['check8'])) { echo 'checked="checked"'; } ?> />
</div>
My requests :
What is the most efficient way of doing this? (simple without complicating the problem)
Any sample code is greatly appreciated.
I think this is what you're looking for. You can achieve it using .index() to get current clicked checkbox. .slice() is used to get all elements at index 4 and after.
$('input[type=checkbox]').change(function(){
var $linputs = $('input[type=checkbox]').slice(4);
var $this = $(this);
$linputs.prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$linputs.prop('checked',false);
}
});
FIDDDLE
Or is it something like this that you want? Where only one of the first four checkboxes can be checked. If one is checked then all the others will be disabled.
$('input[type=checkbox]').change(function(){
var $this = $(this);
$(linputs).prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$(linputs).prop('checked',false);
}
});
http://jsfiddle.net/h5wDr/
EDIT:
if you have other checkboxes in the page and want to be able to separate them from this logic, you can add context in the selector so it keeps this code isolated to only those within this div like so
<div id='test'>
<input type="checkbox" name="check1" value="1" id="check1" >first
<input type="checkbox" name="check2" value="1" id="check2" >second
<input type="checkbox" name="check3" value="1" id="check3" >third
<input type="checkbox" name="check4" value="1" id="check4" >fourth
<input type="checkbox" name="check5" value="1" id="check5" >fifth
<input type="checkbox" name="check6" value="1" id="check6" >sixth
<input type="checkbox" name="check7" value="1" id="check7" >seventh
<input type="checkbox" name="check8" value="1" id="check8" >eight
</div>
Then just add the context
var $inputs = $('input[type=checkbox]', $('#test'));
// this will only select checkboxes within the element with id=test
http://jsfiddle.net/h5wDr/2/
<div>
<input type="checkbox" name="check1" value="1" id="check1" />
<input type="checkbox" name="check2" value="1" id="check2" />
<input type="checkbox" name="check3" value="1" id="check3" />
<input type="checkbox" name="check4" value="1" id="check4" />
<input type="checkbox" name="check5" value="1" id="check5" />
<input type="checkbox" name="check6" value="1" id="check6" />
<input type="checkbox" name="check7" value="1" id="check7" />
<input type="checkbox" name="check8" value="1" id="check8" />
</div>
<script>
$(document).ready(function () {
var $firstFourChecks = $("#check1,#check2,#check3,#check4");
var $lastFourChecks = $("#check5,#check6,#check7,#check8");
$firstFourChecks.on('click', function (e) {
var isCheck = $(this).is(':checked');
$firstFourChecks.not($(this)).prop('checked', false);
$(this).prop('checked', isCheck);
if (isCheck) {
$lastFourChecks.prop("disabled", true).prop('checked', false);
} else {
$lastFourChecks.prop("disabled", false);
}
});
});
</script>
This is entirely done in javascript, and is agnostic to the fact you are using php. Essentially, we make sure in the first four for you have not selected, they are set to false. Then we toggle the state of the one clicked.
If you clicked something on in your first four the last four are turned off and disabled, otherwise they are renabled. This matches the posted pseudocode.
You should be able to paste this directly in. The selectors are cached for speed reasons.
http://jsfiddle.net/L4qeN/ see it here.
Edit: wow looks like someone beat me to the punchline by only a few minutes. We did use very different methods; however.
You would have to try it yourself, but I would do something like (using javascript):
Add a class to every checkbox of the first group and another class to every checkbox of the second group;
Check for change events for the first class, turn off all of the first group but the clicked one;
Check if any of the first group is selected and activate / deactivate the second class group accordingly.
Give your first four checkboxes one class and then your second four a second class and then add onclick handlers to all the first checkboxes:
$('.firstfour_class').click(
if $('input:checkbox:checked.firstfour_class').length > 1){
//code to turn OFF the second four and make them unchecked
} else {
//code to turn ON the second four
}
})
Check out the jQuery :checked selector.
I am not very good at programming, I try to make the following radio buttons:
<input type="radio" name="yesno1" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check1">*please select</div>
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check2">*please select</div>
<input type="radio" name="yesno3" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno3" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check3">*please select</div>
<input type="radio" name="yesno5" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno5" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check5">*please select</div>
<input type="button" value="submit" id="submit">
here is my javascript:
$("#submit").click(function(){
if (!$("input[name='yesno1']):checked").val()) {
$("#check1").show();
}
if ($("input[name='yesno1']):checked").val()) {
$("#check1").hide();
}
if (!$("input[name='yesno2']):checked").val()) {
$("#check2").show();
}
if ($("input[name='yesno2']):checked").val()) {
$("#check2").hide();
}
if (!$("input[name='yesno3']):checked").val()) {
$("#check3").show();
}
if ($("input[name='yesno3']):checked").val()) {
$("#check3").hide();
}
if (!$("input[name='yesno5']):checked").val()) {
$("#check5").show();
}
if ($("input[name='yesno5']):checked").val()) {
$("#check5").hide();
}
});
I know I am doing a clumsy way, what I want is that, if only one radio button is checked, the other three error messages should show, if two is checked, the other two should show, if three check , the other one should show, if no one checked, all of them show. However only the last case works at the moment, I don't know where I am doing wrong, can any of you help me, thanks in advance.
In addition, I am going to pass the data to the php file, so is it possible if I do:
$yesno1 = $_POST['yesno1'];
$yesno2 = $_POST['yesno2'];
$yesno3 = $_POST['yesno3'];
$yesno5 = $_POST['yesno5'];
Thanks for the kind help:)
Try this using jQuery is method. Also you can use else block instead of checking for if every time.
$("#submit").click(function(){
if (!$("input[name='yesno1']").is(':checked')) {
$("#check1").show();
}
else{
$("#check1").hide();
}
if (!$("input[name='yesno2']").is(':checked')) {
$("#check2").show();
}
else{
$("#check2").hide();
}
if (!$("input[name='yesno3']").is(':checked')) {
$("#check3").show();
}
else{
$("#check3").hide();
}
if (!$("input[name='yesno5']").is(':checked')) {
$("#check5").show();
}
else{
$("#check5").hide();
}
});
As I am currently working with my project for addcart. I have written the javascript query for radio button can any one check this either my query is wrong or right?
JavaScript:
function get_radio_value()
{
for (var i=0; i < document."make_payment_frm".rmr.length; i++)
{
if (document."make_payment_frm".rmr[i].checked)
{
var rad_val = document."make_payment_frm".rmr[i].value;
}
}
}
HTML:
<input name="rmr" id="rmr" type="radio" value="3" onclick="get_radio_value()">
<input name="rmr" id="rmr" type="radio" value="5" onclick="get_radio_value()">
<input name="rmr" id="rmr" type="radio" value="10" onclick="get_radio_value()">
Can any one give me the correct code? If I click those buttons that amount should be added with addcart amount. For example: if my addcart amount is 27£ then I choose 2nd radio button finaly it should be display like a 32£.
Thanks in advance.
Your syntax is off, it should be document.make_payment_frm, not quotes in there (that's for bracket notation, e.g. document["make_payment_frm"]). Also, IDs shouldn't be repeated, your inputs should just be:
<input name="rmr" type="radio" value="3" onclick="get_radio_value()" />
<input name="rmr" type="radio" value="5" onclick="get_radio_value()" />
<input name="rmr" type="radio" value="10" onclick="get_radio_value()" />
Then matching script to get the value:
function get_radio_value() {
var rad_val = 0;
for (var i=0; i < document.make_payment_frm.rmr.length; i++) {
if (document.make_payment_frm.rmr[i].checked) {
rad_val = document.make_payment_frm.rmr[i].value;
break;
}
}
//use rad_val here to add to whatever
}
You can test it out here.
But since you tagged this jQuery, why not take full advantage of it? Remove those onclick handlers and add a click or change handler, like this:
$(function() {
$("input[name='rmr']").change(function() {
var val = +$(this).val();
//use val, it's a number ready to add to
});
});
You can test that version here.