473,671 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.someFo rm.someCheckbox .checked = true;
}
<form name="someForm" >
<input type="checkbox" name="someCheck box" value="1"
onclick="checkF orCheck();">
</form>
Thank you!

Jun 20 '06 #1
7 4769
just disable the checkbox.
document.getEle mentById('check Box').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.someFo rm.someCheckbox .checked = true;
}
<form name="someForm" >
<input type="checkbox" name="someCheck box" value="1"
onclick="checkF orCheck();">
</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.someFo rm.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.someFo rm.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.ne t.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.someFo rm.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.someFo rm.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.someFo rm.someCheckbox ;
var browserName=nav igator.appName. toLowerCase();
if (browserName==" netscape") {
cb.onclick = function () {
return false;
}
} else {
if (browserName==" microsoft internet explorer") {
cb.attachEvent( "onclick",retur nFalse);
} else {
//other browsers
}
}
</script>

Jun 21 '06 #5
RHPT wrote:
On 20 Jun 2006 00:00:44 -0700, "RobG" <rg***@iinet.ne t.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.someFo rm.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.someFo rm.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.someFo rm.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;check ed<input type="checkbox" name="cb0" onclick="
this.form.cb1.c hecked = 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*********@gm ail.com>
wrote:

:Try this, after creating the checkbox.
:
:<script type="text/javascript">
:function returnFalse() {
: return false;
:}
:var cb = document.someFo rm.someCheckbox ;
:var browserName=nav igator.appName. toLowerCase();
:if (browserName==" netscape") {
: cb.onclick = function () {
: return false;
: }
:} else {
: if (browserName==" microsoft internet explorer") {
: cb.attachEvent( "onclick",retur nFalse);
: } 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*********@gm ail.com>
wrote:

:Try this, after creating the checkbox.
:
:<script type="text/javascript">
:function returnFalse() {
: return false;
:}
:var cb = document.someFo rm.someCheckbox ;
:var browserName=nav igator.appName. toLowerCase();
:if (browserName==" netscape") {
: cb.onclick = function () {
: return false;
: }
:} else {
: if (browserName==" microsoft internet explorer") {
: cb.attachEvent( "onclick",retur nFalse);
: } 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",retur nFalse);
} 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
3332
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) The number of rows in the table depends on number of records in a database. Therefore, the number of buttons is variable.
2
3264
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) to fire the filter event. Currently if no records are found that match criteria, the page just stays empty. I would like the user to receive a message if no records were found for the dates provided. I spent a lot of time attempting to add a...
7
13207
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 = document.createElement('a'); dom_obj.setAttribute('href', 'javascript:void(0)'); dom_obj.setAttribute('onclick', 'javascript:test()');
3
1862
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, txtNode, imgNode, ext; for (var i=0;i<aryFiles.length;i++) { node = document.createElement('div'); node.className='file';
2
1345
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 event created of dyanamically created button. Please help me, if any one having solution for my problem. I know that i can raise that event if i recreate these controls on page load but my reqirements doesn't allow me to do so. My code for...
0
1423
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 events survive postback. in the user control i have a datagrid which uses the old databinding events (updating, editing etc,...). Here's the problem, the editing event fires but the updating event doen't. Take a look - This is the...
5
4295
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 delegate: public class DelegateEx {
3
2732
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 the latter GUI tool first! I must quote first that I am a novice user of python so the issue(s) I have may seem very obvious but please be patient with me! I have noticed that all the wxpython references I used for creating my
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.