473,385 Members | 1,400 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,385 software developers and data experts.

Dynamicall Creating an OnClick event

I have a checkbox that I dynamically check (via JavaScript) when the
user initiates an action on the page. I want to prevent the user from
unchecking the box if that action has been initiated and the user has
not canceled it. Essentially, I want to dynamically create an
onclick="return false;" event on the checkbox, but only when the user
initiates this particular action.

My attempts at doing this have not worked out. I tried checking to see
if the action occurred, then checking the box via javascript. However,
I find this does not work . Does anyone know how I can achieve this?
Do I even make any sense at all? I have provided some pseudo code
below.

function checkForCheck()
{
if (this_condition == true)
document.someForm.someCheckbox.checked = true;
}
<form name="someForm">
<input type="checkbox" name="someCheckbox" value="1"
onclick="checkForCheck();">
</form>
Thank you!

Jun 20 '06 #1
7 4757
just disable the checkbox.
document.getElementById('checkBox').disabled = true;

RHPT написа:
I have a checkbox that I dynamically check (via JavaScript) when the
user initiates an action on the page. I want to prevent the user from
unchecking the box if that action has been initiated and the user has
not canceled it. Essentially, I want to dynamically create an
onclick="return false;" event on the checkbox, but only when the user
initiates this particular action.

My attempts at doing this have not worked out. I tried checking to see
if the action occurred, then checking the box via javascript. However,
I find this does not work . Does anyone know how I can achieve this?
Do I even make any sense at all? I have provided some pseudo code
below.

function checkForCheck()
{
if (this_condition == true)
document.someForm.someCheckbox.checked = true;
}
<form name="someForm">
<input type="checkbox" name="someCheckbox" value="1"
onclick="checkForCheck();">
</form>


Thank you!


Jun 20 '06 #2
RHPT wrote:
I have a checkbox that I dynamically check (via JavaScript) when the
user initiates an action on the page. I want to prevent the user from
unchecking the box if that action has been initiated and the user has
not canceled it. Essentially, I want to dynamically create an
onclick="return false;" event on the checkbox, but only when the user
initiates this particular action.

My attempts at doing this have not worked out. I tried checking to see
if the action occurred, then checking the box via javascript. However,
I find this does not work . Does anyone know how I can achieve this?
Do I even make any sense at all? I have provided some pseudo code
below.

function checkForCheck()
{
if (this_condition == true)
document.someForm.someCheckbox.checked = true;


If this_condition is a boolean or an expression that evaluates to
true/false, forget the 'if' and do:

var cb = document.someForm.someCheckbox;
cb.checked = this_condition;
cb.disabled = cb.checked;
[...]

--
Rob

Jun 20 '06 #3
On 20 Jun 2006 00:00:44 -0700, "RobG" <rg***@iinet.net.au> wrote:

:RHPT wrote:
:> I have a checkbox that I dynamically check (via JavaScript) when the
:> user initiates an action on the page. I want to prevent the user from
:> unchecking the box if that action has been initiated and the user has
:> not canceled it. Essentially, I want to dynamically create an
:> onclick="return false;" event on the checkbox, but only when the user
:> initiates this particular action.
:>
:> My attempts at doing this have not worked out. I tried checking to see
:> if the action occurred, then checking the box via javascript. However,
:> I find this does not work . Does anyone know how I can achieve this?
:> Do I even make any sense at all? I have provided some pseudo code
:> below.
:>
:> function checkForCheck()
:> {
:> if (this_condition == true)
:> document.someForm.someCheckbox.checked = true;
:
:If this_condition is a boolean or an expression that evaluates to
:true/false, forget the 'if' and do:
:
: var cb = document.someForm.someCheckbox;
: cb.checked = this_condition;
: cb.disabled = cb.checked;
:
:
:[...]
I tried what you suggested already, but it still did not work. It's
almost as if the browser (I'm using IE) will not re-check the box once
it is un-checked. Also, I cannot disable the textbox because then
ColdFusion will not "see" it as s form field when I submit the form
for processing.
Jun 21 '06 #4
Try this, after creating the checkbox.

<script type="text/javascript">
function returnFalse() {
return false;
}
var cb = document.someForm.someCheckbox;
var browserName=navigator.appName.toLowerCase();
if (browserName=="netscape") {
cb.onclick = function () {
return false;
}
} else {
if (browserName=="microsoft internet explorer") {
cb.attachEvent("onclick",returnFalse);
} else {
//other browsers
}
}
</script>

Jun 21 '06 #5
RHPT wrote:
On 20 Jun 2006 00:00:44 -0700, "RobG" <rg***@iinet.net.au> wrote:

:RHPT wrote:
:> I have a checkbox that I dynamically check (via JavaScript) when the
:> user initiates an action on the page. I want to prevent the user from
:> unchecking the box if that action has been initiated and the user has
:> not canceled it. Essentially, I want to dynamically create an
:> onclick="return false;" event on the checkbox, but only when the user
:> initiates this particular action.
:>
:> My attempts at doing this have not worked out. I tried checking to see
:> if the action occurred, then checking the box via javascript. However,
:> I find this does not work . Does anyone know how I can achieve this?
:> Do I even make any sense at all? I have provided some pseudo code
:> below.
:>
:> function checkForCheck()
:> {
:> if (this_condition == true)
:> document.someForm.someCheckbox.checked = true;
:
:If this_condition is a boolean or an expression that evaluates to
:true/false, forget the 'if' and do:
:
: var cb = document.someForm.someCheckbox;
: cb.checked = this_condition;
: cb.disabled = cb.checked;
:
:
:[...]
I tried what you suggested already, but it still did not work. It's
almost as if the browser (I'm using IE) will not re-check the box once
it is un-checked. Also, I cannot disable the textbox because then
ColdFusion will not "see" it as s form field when I submit the form
for processing.


Then don't disable it. Give it an onclick handler that checks
'this_condition' to determine whether it should be checked or not, e.g.

if (this_condition) document.someForm.someCheckbox.checked = true;

Whenever the user clicks on it, 'this_condition' is checked. That might
be a statement that checks the status of some other control or element,
or whatever. If it returns false, the script doesn't interfere with
whether the checkbox is checked or not.

e.g.

<form action="">
Keep&nbsp;checked<input type="checkbox" name="cb0" onclick="
this.form.cb1.checked = true;
">
<input type="checkbox" name="cb1" onclick="
if (this.form.cb0.checked) this.checked = true;
">
</form>
--
Rob
Jun 21 '06 #6
On 21 Jun 2006 00:57:58 -0700, "Botan Guner" <bo*********@gmail.com>
wrote:

:Try this, after creating the checkbox.
:
:<script type="text/javascript">
:function returnFalse() {
: return false;
:}
:var cb = document.someForm.someCheckbox;
:var browserName=navigator.appName.toLowerCase();
:if (browserName=="netscape") {
: cb.onclick = function () {
: return false;
: }
:} else {
: if (browserName=="microsoft internet explorer") {
: cb.attachEvent("onclick",returnFalse);
: } else {
: //other browsers
: }
:}
:</script>
That worked great! Thanks!
Jun 22 '06 #7
RHPT wrote:
On 21 Jun 2006 00:57:58 -0700, "Botan Guner" <bo*********@gmail.com>
wrote:

:Try this, after creating the checkbox.
:
:<script type="text/javascript">
:function returnFalse() {
: return false;
:}
:var cb = document.someForm.someCheckbox;
:var browserName=navigator.appName.toLowerCase();
:if (browserName=="netscape") {
: cb.onclick = function () {
: return false;
: }
:} else {
: if (browserName=="microsoft internet explorer") {
: cb.attachEvent("onclick",returnFalse);
: } else {
: //other browsers
: }
:}
:</script>
That worked great! Thanks!


If you're happy to implement your function using probably the *worst*
algorithm possible, then stick with it.

But then ask yourself why browsers like Mozilla, Firefox, Safari, et al
identify themselves as 'Nestscpe' and Opera pretends to be IE. The
string is utterly useless for identifying which browser is being used.

If you want to discriminate based on event model, why not:

var cb = document.forms['someForm'].elements['someCheckbox'];
if (cb.attachEvent) {
cb.attachEvent("onclick",returnFalse);
} else {
cb.onclick = function () {return false;};
}

Less code too.

Though I have to wonder why you have a checkbox that the user can't
modify. Implement at the server whatever logic you use on the client to
check the checkbox and you don't need any client-side script at all (for
this).
--
Rob
Jun 22 '06 #8

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

Similar topics

1
by: Hrvoje Vrbanc | last post by:
Hello all! My question is the following: I add buttons (server controls) programatically into a table cell. Example: Dim btObnovi As Button = New Button() celija25.Controls.Add(btObnovi) ...
2
by: zeg37 | last post by:
Hi, I am working with a Microsoft Data Page where the records are filtered by two dates the user selects prior to any records being loaded. After selecting dates they click on a button (BtnGo)...
7
by: Moses | last post by:
Hi Everybody, I have a problem with onClick event which works in FF and does not work in IE, Here I have giving the details Please help. I am creating a <aTag. dom_obj =...
3
by: Cylix | last post by:
my function: function displayFiles(files, curPath) { var d = document.getElementById('folderContent'); d.innerHTML = ''; if (!(d&&files)) return; var aryFiles = files.split('|'); var node,...
2
by: Hellogeetu | last post by:
Hi All, I am creating dynamic controls but not on page_load event but on one of the button click event. I m able to get the controls value by using request.form but i m unable to handle the...
0
by: stimul8d | last post by:
Before i get flamed, this isn't the usual question you see left right and center. I'm dynamically creating usercontrols inside the page_init event and setting the ID's of each control so that the...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
3
by: lee.walczak | last post by:
Hi, I have just started writing a GUI using wxpython after finding a limitation using Tkinter. I have read most tutorials on wxpython and slowly becoming accustomed considering I started with...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.