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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

butt.attachEvent('onclick',doSomething); Not Working

Status
Not open for further replies.

logincafe

Programmer
Nov 25, 2006
4
CA
Hi everyone;

I am trying to attachEvent to a button so when you click on the button it is handled by my function doSomething(). Everything else works except the button when I click it - it does nothing. Any suggestions?

var mycurrent = top.frames['bottomFrame'].document.createElement("td");
var butt = top.frames['bottomFrame'].document.createElement("BUTTON");
butt.setAttribute('id', 'test');
butt.setAttribute('type','button');
butt.setAttribute('value','X');
mycurrent.appendChild(butt);
butt.attachEvent('onclick',doSomething);
mycurrent_row.appendChild(mycurrent);

// appends the row <tr> into <tbody>
mytablebody.appendChild(mycurrent_row);
 
It could be that your browser doesn't support attachEvent... Try changing this:

Code:
butt.attachEvent('onclick',doSomething);

to this:

Code:
if (window.addEventListener) {
    butt.addEventListener('click', doSomething, false);
} else if (window.attachEvent) {
    butt.attachEvent('onclick', doSomething);
}


Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Still no luck everything appears but when I click on the button nothing happens..... I am using IE6 and IE7 to display. Any other suggestions please.... Thank you!
Code:
                var mycurrent = top.frames['bottomFrame'].document.createElement("td");    
                var butt = top.frames['bottomFrame'].document.createElement("BUTTON");
                butt.setAttribute('id', 'test');
                butt.setAttribute('type','button');            
                butt.setAttribute('value','X');
                mycurrent.appendChild(butt);
                if (window.addEventListener) 
				{
    				butt.addEventListener('click', doSomething, false);
				}
				else if (window.attachEvent) 
				{
    				butt.attachEvent('onclick', doSomething);
				}
                mycurrent_row.appendChild(mycurrent);
                
                // appends the row <tr> into <tbody>
                mytablebody.appendChild(mycurrent_row);
 
Hi Dan;

I have tried FireFox and it does not work in this browser.

I do not have a URL for this page but I can send you more code.

Thanks Dan for helping me ... I have been working on this problem for a day and a half.

Cheers;

Gary

Code:
function setnew_table() 
{
        // get the reference for the body
		addToCounter();
		var counter = readCookie('myCounter');
		counter = parseInt(counter);
		
        var mybody = top.frames['bottomFrame'].document.getElementsByTagName("body")[0];
		
        // creates a <table> element and a <tbody> element
        mytable     = top.frames['bottomFrame'].document.createElement("table");
		//mytable.setElementById('id');
		mytable.setAttribute('width','200');
		mytable.setAttribute('id', counter);
		
	
        mytablebody = top.frames['bottomFrame'].document.createElement("tbody");
		
		
		
		if ( counter >= 0 )
		{
        	// creating all cells
        	for(var j = 0; j < 1; j++) 
			{
            	// creates a <tr> element
            	mycurrent_row = top.frames['bottomFrame'].document.createElement("tr");
			
            	for(var i = 0; i < numbers.length; i++) 
				{
                	// creates a <td> element
                	var mycurrent_cell = top.frames['bottomFrame'].document.createElement("td");
					mycurrent_cell.setAttribute('width','17%');
					mycurrent_cell.setAttribute('align','center');
                	// creates a text node
                	currenttext = top.frames['bottomFrame'].document.createTextNode(numbers[i]);
                	// appends the text node we created into the cell <td>
                	mycurrent_cell.appendChild(currenttext);
                	// appends the cell <td> into the row <tr>
                	mycurrent_row.appendChild(mycurrent_cell);
            	}
				;
				var mycurrent = top.frames['bottomFrame'].document.createElement("td");	
				var butt = top.frames['bottomFrame'].document.createElement("BUTTON");
				butt.setAttribute('id', 'test');
				butt.setAttribute('type','button');
				//butt.setAttribute('name','test');
				butt.setAttribute('value','X');
               	//butt.attachEvent('onclick',doSomething);
				if (window.addEventListener) 
				{
					alert("Spencer");
    				butt.addEventListener('click', doSomething, false);
				}
				else if (window.attachEvent) 
				{
    				alert("Jessica");
					butt.attachEvent('onclick', doSomething);
					alert(butt.onclick);
				}

				
				mycurrent.appendChild(butt);
				
				//butt.attachEvent('onclick',doSomething);
				mycurrent_row.appendChild(mycurrent);
			
				
            	// appends the row <tr> into <tbody>
            	mytablebody.appendChild(mycurrent_row);
				

				
        	}
			
        	// appends <tbody> into <table>
        	mytable.appendChild(mytablebody);
        	// appends <table> into <body>
        	mybody.appendChild(mytable);
			
			
        	// sets the border attribute of mytable to 2;
        	mytable.setAttribute("border", "0");
			
		
		}
		
		

}
function doSomething()
{
	alert("Jen");
}
 
A URL would be the best way - I've no idea of the context this code is being run in - presumably you have a whole frameset setup, which would probably take more code than it's worth posting to replicate exactly.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
> for(var j = 0; j < [highlight]1[/highlight]; j++)
Make sure 1 is what you want, maybe not counter?

>for(var i = 0; i < [highlight]numbers[/highlight].length; i++)
numbers is not defined. It should be some sort of array.

With those corrected, it should work or very close to.
 
Thank you Dan;

To get the button to work I inserted this code and it started working! I would like to thank you again for your time and effort...

Cheers;
Gary

Code:
var butt = document.createElement("<input type='button' value='X' onclick='doSomething()'/>");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top