/    Sign up×
Community /Pin to ProfileBookmark

Problem with onclick event sending variables to functions

Hi all, I have a problem that when I try to send any variables to a function using onclick, the checkbox doesn’t display.

Here is the example code:

[CODE]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>CheckBoxes</title>
</head>
<body>
<FORM Name=”myForm” Action=”” Method=”POST”>
</FORM>
</body>
<HEAD><META HTTP-EQUIV=”PRAGMA” CONTENT=”NO-CACHE”></HEAD><script language=”JavaScript” type=”text/javascript”>/**
* @author 3p
*/

var mytable = document.createElement(“Table”);
mytable.style.color = “#000000”;
mytable.border = “1”;
mytable.cellPadding =”5″;
mytable.style.borderWidth = “2”;
mytable.style.borderColor = “#000000”;

function addCheck(ID, captn, desc)
{
var newrow=mytable.insertRow(-1) //add new row to end of table
var chx = document.createElement(“Input”);
chx.setAttribute(“type”, “checkbox”);
chx.name= “perms”;
chx.value = ID;
chx.id = “C”+ID;
chx.style.backgroundColor = “#00FF00”;
chx.onclick = updatePermissions; //updatePermissions(ID); doesn’t work
//chx.addEventListener(‘click’, updatePermissions(ID), false);
//chx.attachEvent(“onclick”, updatePermissions(ID));
newcell=newrow.insertCell(0) //insert new cell to row
newcell.align = “center”;
newcell.appendChild(chx);
}

for (i=0; i< 2; i++) {
addCheck(i, i, i);
}

document.myForm.appendChild(mytable);

function updateT() {
updatePermissions(1);
}

function updatePermissions(chx) {
//Do Something with the data
}

function rePos() {
mytable.style.position = “absolute”;
mytable.style.top = myForm.offsetTop;
mytable.style.left = 30;
}

rePos();
</script>
</html>
[/CODE]

Fairly straight forward, we make a table populate it with a couple of checkboxes.

Now I really want to have the line of code

[CODE]chx.onclick = updatePermissions;[/CODE]

looking more like:

[CODE]chx.onclick = updatePermissions(ID);[/CODE]

But If I do this, then none of the javascript displays on the page. (I have tried, IE7 and Maxthon2 UA’s).

I need a soloution to this as I need to have the form dynamically built. I have no Idea how many checkboxes there will be.

Cheers people.

Cheers!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 24.2007 — use an anonymous and a closure:
<i>
</i>chx.onclick=function(){updatePermissions(this.value)}
Copy linkTweet thisAlerts:
@Banana_AnandaJul 24.2007 — If you leave it as it is: ...onclick = updatePermissions; Then the keyword [COLOR="Blue"]this [/COLOR]inside the function will be a reference to the element clicked. If you need the actual ID, then all you need to do i remove the "C" at the beginning of [COLOR="Blue"]this[/COLOR].id
×

Success!

Help @3pc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...