Dynamicall Creating an OnClick event

R

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!
 
C

chonny

just disable the checkbox.
document.getElementById('checkBox').disabled = true;



RHPT напиÑа:
 
R

RobG

RHPT said:
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;


[...]
 
R

RHPT

: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.
 
B

Botan Guner

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>
 
R

RobG

RHPT said:
: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>
 
R

RHPT

On 21 Jun 2006 00:57:58 -0700, "Botan Guner" <[email protected]>
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!
 
R

RobG

RHPT said:
On 21 Jun 2006 00:57:58 -0700, "Botan Guner" <[email protected]>
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,569
Members
47,205
Latest member
KelleM857

Latest Threads

Top