how to bind click event to checkbox label - javascript

I'm hoping a javascript / jquery guru can help me out.
I'm trying to figure out a way to launch a popup box (fancybox) from the click of a checkbox label. I don't want to effect the checkbox function which filters data when clicked.
I would welcome any thoughts on how I can make this happen. I've tried an onclick function on the label but that doesn't work.
I want to show a fancybox when you click on a persons name in the checkbox label. Here's the site:- (example: jonathanlyon)
http://monkeygetnews.jonathanlyon.com/bs.php
The checkboxes are built dynamically and I need to create a click event on the label that opens a fancybox. Here's the code to build the checkboxes:-
<input type="checkbox" id = "dn" value="dept_<?php echo $currdept;?>" onchange="doSearch();return false;" />
<label><?php echo $currdept;?></label>
<ul class="dept_<?php echo $currdept;?>">
<li>
<input type="checkbox" name = "un" value="<?php echo $usernames;?>" <?php echo $include;?> onchange="doSearch();return false;" />
<label><?php echo $usernames;?></label>
</li>
<?php
$dept = $currdept;
} else {
?>
<li>
<input type="checkbox" name = "un" value="<?php echo $usernames;?>" <?php echo $include;?> onchange="doSearch();return false;" />
<label><?php echo $usernames;?></label>
</li>
<?php
$dept = $currdept;
}
?>
Thanks
Jonathan

Very simply put you can use jquery like this
$('label').click(function() {
alert($(this).html());
}
again, this is a very simple example and would need improvement

You need to wrap checkbox tag with label tag to make label clickable like
<label><input type="checkbox" name="checkbox" value="value"/>dasfaha</label>
Edit:
Sorry guys. I didn't look carefully. Here is how to detect label or checkbox.
How it works is that when you click unbounded label, checkbox won't be changed. However, when you click checkbox, ':checked' will be changed. Thus, you can put a variable to store previous value of checked, then compare with current checked.
http://jsfiddle.net/Xtyah/

Related

how to Keep check box value after refresh the page for arrays like name="check[0]"

I have 2 check boxes and I want to keep the check box value after page refresh too. I mean if I enable a check box and after I submit the form if I navigate back or refresh the page it should be enable only.
I tried this but not working.
<input type="checkbox" id="enable1" name="check[0]" value= "1"
<?php if (in_array("0", $_POST['check'])) echo "checked='checked'"; ?> />
<input type="checkbox" id="enable2" name="check[1]" value= "1"
<?php if (in_array("1", $_POST['check'])) echo "checked='checked'"; ?> />
Please help me with this how to keep the state of check boxes after page reload
thanks..

Retain value of radio button after validation php

My issue is once user click submit the form should check for all questions that the radio button is checked.
It should show the err message on the questions where the user didn't answer.
But right now the form refreshed everytime after the err message is shown and the
previous checked radio button is lost.I also have javascript timer going on,but
since the page get refreshed ,the timer starts all over again.
Im newbie to php,kindly guide me through this issue.Any help is appreciated.
<?php session_start()?>
<?php
$errorexist=false;
if (isset($_POST["submit"])){
for ($y=0;$y<5;$y++)
{
if(empty($_POST["question".$y])){
$errorexist=true;
${"err".$y}="* Please answer question ".($y+1)."<br>";
}
}
if (errorexist==false){
header ("Location: result.php");
exit;}
} ?>
html part of the code
<li>
<span class="error"><?php echo $err0?></span>
<h3>What is the smallest prime number?</h3>
<input type="radio" name="question0" value="A"<?php echo ($question0=='A')? 'checked':''; ?> />2<br>
<input type="radio" name="question0" value="B"<?php echo ($question0=='B')? 'checked':''; ?> />1<br>
<input type="radio" name="question0" value="C"<?php echo ($question0=='C')? 'checked':''; ?> />3<br>
<input type="radio" name="question0" value="D"<?php echo ($question0=='D')? 'checked':''; ?> />4<br>
</li>
Put a condition, where if any radio button misses it displays the
error message and no need to refresh the page. Inside the PHP code,
Request submit button
Request all the radio button. i.e $question1 = $_REQUEST['question1'];
check if all the values are set and not empty if(isset($question1) && !empty($question1) && $question1 !== '')
do this for all, close this condition with the error message.
This might help.

checkboxes behave like radiobuttons with different names

i have a problem, which might be easy for you guys. Im new in jquery and trying to do a form (calculator) of selectboxes, checkboxes and 3 radiobuttons (with 3 same names, just one or none can be checkable).
When i submit the form (at the first time), the values of each inputs stay the same as I wanted to. But when I submit it the second time without refreshing/resetting, the radiobutton which was checked before disappears and even when I reclick a radiobutton without refreshing and submit it, the value cant maintained by php.
So I decided to change the radio buttons to checkboxes, and that is where I need your help.
I need 3 checkboxes which can be selected just one or none of them with different "names".
<input type="checkbox" id="checkbox8" value="1" name="8" <?php if(isset($_POST['8']) && $_POST['8'] == '1') echo 'checked="checked"';?> />
<input type="checkbox" id="checkbox9" value="2" name="9" <?php if(isset($_POST['9']) && $_POST['9'] == '2') echo 'checked="checked" ';?> />
<input type="checkbox" id="checkbox10" value="3" name="10" <?php if(isset($_POST['10']) && $_POST['10'] == '3') echo 'checked="checked" ';?> />
I prepared this:
[a link] (http://jsfiddle.net/ry4k8ve9/)
thanks a lot in advance!! Hope you can help me
Just add a click event to your checkboxes to turn the others off:
$('input[type="checkbox"]').click(function(){
$('input[type="checkbox"]').not(this).prop('checked', false)
})
try this:-
var $unique = $('#checkbox8,#checkbox9,#checkbox10');
$unique.click(function() {
$unique.not(this).removeAttr('checked');
});
Demo

retaining checkbox and POP Up to add more

I have made a form in which a person puts up his details.
After completing the form and submitting it, I want a popup which should ask "Do you want to add more?","Yes or No ?".
If Yes is chosen, the some values filled in the form should be retained and the rest should appear blank.
On NO the page should get freshened with everything blank.
I am able to retain the values of the drop down items and text fields,but I am not able to retain the values of the check box items.
My check box code :
<label>Hotel Facilities</label>
</td><td class="row1"> :</td> <td class="row1" width="70%">
<div style=" font-weight:normal; color: #4B4B4B;font-size: 12px; width:260px; background:#D2D2D2; max-height:300px; overflow:scroll;overflow-x:hidden;">
<?php $dcs=mysql_query("select * from `tblfacility` where `Type`='Rm'");
while($dc=mysql_fetch_array($dcs)){?>
<input type="checkbox" name="check_list[]" value="<?php echo $dc['FId']; ?>">
<?php echo $dc['Facility'];?> <br />
<?php }?>
</div>
I tried to retain by using:
<input name="check_list[]" type="checkbox" value="<?php echo $dc['FId']; ?>" <?php if(isset($_POST['check_list[]'])) echo "checked='checked'"; else echo $dc['Facility']; ?>
but it did not worked
Please help me how to retain its values after submitting that page.
And the pop up I am at present using is just an alert message and hence it just have OK button:
<?php
if(isset($_POST['submit'])){
echo "<script>alert ('Do You want to Add More ?');</script>";
$HId=$_POST['HId'];
?>
hence it is just showing OK button after submitting .
Please guide how to put Yes or No button after submitting hence is Yes some values will be retained and if NO the whole from will be refreshed.
Instead of checked='checked' just use "selected" or "selected=true". I hope it works!

<Label> tag alternative for Javascript

I have the following Problem
I get a variable from PHP via:
echo $var
Actually this takes place in:
<a href:"..\test.php?go=<?php echo $var ?>">
And to a page where you can get more info about $var...
Today i learned about the display attribute from a div, so i can display the info via Ajax direct in my actual page. So, I tried:
<Label id="123" onclick="doJS(<?php echo $var ?>)> Click ME </Label>
But this doesn't work, I cant click the label, just nothing happens.
How can this be solved?
You have a typo in your code.
Replace
<Label id="123" onclick="doJS(<?php echo $var ?>)> Click ME </Label>
By
<Label id="123" onclick="doJS(<?php echo $var ?>)"> Click ME </Label>
So you should try:
<label id='123' onclick="doJS(<?=$_GET['go']?>)"> Click ME </label>

Categories

Resources