473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Simple Toggle checkbox function on IMAGE click

I have a checkbox with an ID of svc_tp_1, and an image that
corresponds with this checkbox below it.
<input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" /
>
<img src="images/screen_print.jpg" onclick="toggle('svc_tp_1'); return
true;" />

I know how to get the checkbox to check when the image is clicked, but
what I really want is a toggle. When a user clicks on the image, the
system should first check to see if svc_tp_1 is checked or NOT
checked. If it is not checked, then I'd like for it to be checked.

If it is already checked, then I want the checkbox to go away. Simple
enough? PHP and Javascript are so different that something so simple
like this can waste a lot of my time.
function toggle(me) {

if (me == checked) {
me.checked = checked;

} else {
me.checked = unchecked;
}

}

Jul 21 '08 #1
4 6117
On Jul 22, 2:12 am, ameshkin <amir.mesh...@gmail.comwrote:
I have a checkbox with an ID of svc_tp_1, and an image that
corresponds with this checkbox below it.
<input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" /

<img src="images/screen_print.jpg" onclick="toggle('svc_tp_1'); return
true;" />

I know how to get the checkbox to check when the image is clicked, but
what I really want is a toggle. When a user clicks on the image, the
system should first check to see if svc_tp_1 is checked or NOT
checked. If it is not checked, then I'd like for it to be checked.

If it is already checked, then I want the checkbox to go away. Simple
enough? PHP and Javascript are so different that something so simple
like this can waste a lot of my time.

function toggle(me) {

if (me == checked) {
me.checked = checked;

} else {

me.checked = unchecked;

}
}
Hi,
recently I have done some thing that you want to implement, but its
not the exactly the same code that you want. But i think this can
solve your problem of toggle.

<script language="javascript" type="text/javascript">
var flag=false;
function Add(chkid)
{

if(flag==false)
{
document.getElementById(chkid).checked=true;
flag=true;
}
else
{
document.getElementById(chkid).checked=false;
flag=false;
}

}
</script>

Yo can call this function on Image click.
Jul 22 '08 #2
ameshkin escribió:
I have a checkbox with an ID of svc_tp_1, and an image that
corresponds with this checkbox below it.
<input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" /
<img src="images/screen_print.jpg" onclick="toggle('svc_tp_1'); return
true;" />
function toggle(ckeckbox_id){
var checkbox = document.getElementById(ckeckbox_id);
checkbox.checked = !checkbox.checked;
}

Or you can simply surround your picture in a <labeltag:

<input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" />
<label for="svc_tp_1"><img src="images/screen_print.jpg" /></label>
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jul 22 '08 #3
In comp.lang.javascript message <ededf0a9-fa5c-4078-a91e-527ba22ed85a@p2
5g2000pri.googlegroups.com>, Mon, 21 Jul 2008 14:12:41, ameshkin
<am**********@gmail.composted:
>
If it is already checked, then I want the checkbox to go away. Simple
enough?
No. Evidently you want it to be unchecked, not to vanish. Try to write
clear and exact English, even if you are American.

Consider the implications of

<input type=checkbox ID=X>
<input type=button onClick="document.getElementById('X').checked ^= 1">

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jul 22 '08 #4
In comp.lang.javascript message <17ff2a9f-cae4-419a-84bb-060d79912402@k3
0g2000hse.googlegroups.com>, Mon, 21 Jul 2008 23:42:19, smartwebcoder
<sm***********@gmail.composted:
>
<script language="javascript" type="text/javascript">
^^^^^^^^^^^^^^^^^^^^^ <- superfluous and deprecated
var flag=false;
function Add(chkid)
{

if(flag==false)
{
document.getElementById(chkid).checked=true;
flag=true;
}
else
{
document.getElementById(chkid).checked=false;
flag=false;
}

}
</script>

You are posting with a false identity : no-one smart would consider that
code to be worth posting. Remember - the inadequate boast of their
prowess, the competent just display it.

There is no need to use == false since JavaScript has a NOT
operator, '!'; and if you don't like that you can reverse the then and
else parts.

But there is no need to use the if at all;

{ document.getElementById(chkid).checked = flag = ! flag }

should be equivalent to the body of your function Add .

Firefox 3.0.1 is out.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Jul 22 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jürgen Heyn | last post by:
Good morning, on form 2 input elements and 1 image are placed. The 1st (Index) one is "hidden", the 2nd ) is an empty textbox. When clicking the picture I would like to toogle a boolean value....
7
by: Phil Powell | last post by:
I am producing a form using PHP on the back end and Javascript on the front end. The resulting script will come to the browser as follows: <script> <!-- function selectAll() { moveElement =...
5
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
2
by: John Smith | last post by:
How can I use a transparent command button to change a checkbox? I have my records set up in a continuous form and I want theuser to be able to click on the record and have this toggle the...
1
by: Mike Bahr | last post by:
Hi All, Im not very well versed in javascript at all so hoping someone can help me out here. I have a page that uses pairs of radio buttons to toggle visibility of some table columns. I need...
3
by: mrlk | last post by:
i have the following but would like to use a img button rather than a checkbox - is this possible? function toggleZoom(isChecked) { if (isChecked) { ...
6
by: plumba | last post by:
Hi I have a check box which when ticked displays the sumit buttons of a form and gives the checkbox field a value of "agree". I would like to set the check box to hide the section of the form...
4
by: zufie | last post by:
I have two checkboxes on a form, ChckIBCCP and ChckOtherReferral. Each checkbox highlights its respective textboxes and combo boxes on the form. Many of the highlighted textboxes and combo...
1
by: student4lifer | last post by:
Hello, I currently have a hyperlink to toggle the node, showing and hiding the list. Could someone show me how to use a checkbox instead of the hyperlink to do the same thing? Thanks. I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.