473,385 Members | 1,523 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.

please help, javascript dynamic checkboxes and events question

Hi everyone. this is my first post here, but i've found quite a bit of great information from reading the forums in the past.

i'm not sure if what i'm trying to do is possible or not, but here it is:

i'm creating a dynamic list of checkboxes, basically to allow access to client information on a user by user basis. i use ajax to bring back a full list of clients, then loop through the list and create a checkbox for each client to basically create a checkboxlist. what i want: on each checkbox click to update the database with either added or removed client access. the problem is that the client can't seem to tell which checkbox was clicked.

here's my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         ...
  3.         var sites = search.GetSiteList().value;
  4.         for (var i = 0; i < sites.length; i++)
  5.         {
  6.             // user search site list
  7.             sel.options.length++;
  8.             sel.options[i].value = sites[i].split("-")[0];
  9.             sel.options[i].text = sites[i].split("-")[1];
  10.  
  11.             if (i > 0) // skip "All"
  12.             {
  13.                 // user site assignment list
  14.                 var clientId = sites[i].split("-")[0];
  15.                 el = document.createElement("input");
  16.                 el.type = "checkbox";
  17.                 el.id = clientId;
  18.                 el.onclick = function(){ChangeUserSiteAccess(clientId)};
  19.                 div.appendChild(el);
  20.                 div.appendChild(document.createTextNode(sites[i].split("-")[1]));
  21.                 div.appendChild(document.createElement("br"));
  22.             }
  23.         }
  24.  
the problem is with creating the onclick event. it doesn't seem to create a different event for each checkbox. when i click a checkbox, the ID that is returned is always from the last checkbox that was created.

is there a way to determine which checkbox was clicked? some quick web searching has people normally looping through the entire list to check the .checked property. this is not a great solution for me since a person could have 25 clients selected and 600 unselected.

thanks in advance for any help.
Oct 18 '07 #1
2 4857
i figured it out. i used a function i found on the interweb for adding events:

Expand|Select|Wrap|Line Numbers
  1. // c&p from http://ejohn.org/projects/flexible-javascript-events/
  2. function addEvent(obj, type, fn) 
  3. {
  4.     if (obj.attachEvent) 
  5.     {
  6.         obj['e' + type + fn] = fn;
  7.         obj[type + fn] = function(){obj['e' + type + fn](window.event);}
  8.         obj.attachEvent('on' + type, obj[type + fn]);
  9.     } 
  10.     else
  11.         obj.addEventListener(type, fn, false);
  12. }
and used it:

Expand|Select|Wrap|Line Numbers
  1. {
  2. ...
  3. addEvent(document.getElementById(clientId), "click", ChangeUserSiteAccess);
  4. ...
  5. }
  6.  
  7. function ChangeUserSiteAccess(e)
  8. {
  9.     var id = e.target.id;
  10.     var checked = document.getElementById(id).checked;
  11.     var userId = document.getElementById("hidUserId").value;
  12.     var status = false;
  13.     if (checked == true)
  14.     {
  15.         // item was checked.  save new site assignment for this user
  16.         status = search.AddSiteForUser(id, userId);
  17.     }
  18.     else
  19.     {
  20.         // item was unchecked.  remove this site assignment for this user
  21.         status = search.RemoveSiteForUser(id, userId);
  22.     }
  23. }
this was pieced together from a few other posts on this forum. thanks.
Oct 18 '07 #2
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Glad to hear that you managed to solve your problem and thanks for posting your solution.

Post again if you have any more questions not already answered somewhere within the forums that you can easily find.
Oct 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
7
by: Julia Briggs | last post by:
Hello World - I admit I'm new to javascript but I've tried for days to find a solution to my problem. Basically I have 3 unique javascript files that do different screen display events that I...
5
by: Deborah V. Gardner | last post by:
I would like to use "Yes" and "No" checkboxes on a subform. The problem is that when I click the Yes checkbox on the subform, all of the checkboxes are checked. Currently, I have a field...
7
by: Stephen | last post by:
I have some code which I call from a custom validator however I seem to have got the logic wrong and im having trouble figuring out how to write my code to get things to work the way I require....
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
5
by: Jimmy | last post by:
Hi I am trying to check whether checkboxes are checked in my user control. I have problems getting the right reference to the checkboxes: <%@ Page language="c#" Codebehind="WebForm2.aspx.cs"...
0
by: louise raisbeck | last post by:
Hi there, I am databinding a datagrid on the onload with the usual !IsPostBack condition. I have to create some dynamic checkboxes, because i dont know how many i will need until the dataset...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Dave Ekhaus | last post by:
hi i'm new to javascript. i'm hoping to get some help and find out if what i want to do is possible and - assuming it is, get some tips on how to accomplish the task. ok - assume i have a...
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...
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
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.