Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function called through onclick - attachEvent?

Status
Not open for further replies.

VmusicV

Programmer
Aug 27, 2005
2
0
0
US
Hi,
First THANKS!!! to those who helped me get the attachEvent working.... it works, but now....
I attached a function - by its name - using attachEvent, for IE browsers.
Now when my check box is clicked it calls this function, but how do I pass it a reference ? see attached code below?

So when my function is called by the onclick event, it will know who or which control called it?

Any ideas?

Thanks,
VmuiscV

------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Test IE attachEvent </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="JavaScript">
function createControls(){
alert('Creating input box and check box');

//add form elements in JS
var theForm = document.forms.tester;

//set var to add a unique number to control name
var next_elem = theForm.elements.length + 1;

//alert("Creating the label for the input box");
var label_inputTitle = document.createTextNode("Enter Input Here");
var span_inputTitle = document.createElement('span');
span_inputTitle.appendChild (label_inputTitle);

//alert("Creating the label for the check box");
var label_cbTitle = document.createTextNode("Check Box Here");
var span_cbTitle = document.createElement('span');
span_cbTitle.appendChild (label_cbTitle);

//create textbox
//alert("Creating the actual input box");
var inputControl = document.createElement('input');
inputControl.setAttribute('name', 'new_ai_heading'+ next_elem );
inputControl.setAttribute('type', 'text');
inputControl.setAttribute('size', '60');


//create checkbox
//alert("Creating the actual check box");
var checkboxControl = document.createElement('input');
checkboxControl.setAttribute('name', 'new_ai_delete'+ next_elem );
checkboxControl.setAttribute('type', 'checkbox');

//add a listner
if (!document.addEventListener){
// MS or IE or les miserables
var didItAttach = checkboxControl.attachEvent('onclick', callDisableCheckBox);
if(didItAttach){
alert('IE specific code to attach event worked');
}else{
alert('IE specific code to attach event failed');
}
}else{
//FireFox...etc.
checkboxControl.addEventListener("onclick", "disableAgendaItem(this.id)", false);

}


//now append the controls
alert('Appending labels and controls to form');
theForm.appendChild(span_inputTitle);
theForm.appendChild(inputControl);
theForm.appendChild(span_cbTitle);
theForm.appendChild(checkboxControl);


} //end of function to add control

//function to call to another function to disable the checkbox
function callDisableCheckBox(cntrl){
alert('Testing One, Two Three.... inside the attachEvent function to call a function to disable the checkbox');
alert('OK..Can somebody tell me how I reference -this- the calling checkbox - whose name is ' + cntrl.name);

}//end function to call another function to disable the checkbox
</script>
</HEAD>

<BODY>
<form name="tester" action="" onSubmit="return isItGood()">
<br>
<input type="button" onClick="createControls()" value="Click Me I'm Irish" name="TestButton">
</form>
</BODY>
</HTML>
 
[tt]var didItAttach = checkboxControl.attachEvent('onclick', function () {callDisableCheckBox(event)});[/tt]
[tt]
function callDisableCheckBox(evt){
var e=(window.event)?window.event:evt;
if (window.event) {
alert(e.srcElement.name); //for ie
} else {
alert(e.target.name); //for ff/nn
}
}//end function to call another function to disable the checkbox
[/tt]
If the section is already restricted to ie as you are using attachEvent whereas your ff/nn version calls something else, you can simply passing none.
[tt]
var didItAttach = checkboxControl.attachEvent('onclick', callDisableCheckBox);[/tt]
[tt]
function callDisableCheckBox(){
alert(window.event.srcElement.name);
}//end function to call another function to disable the checkbox
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top