SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM, setting onclick to button in IE not working

    Hi,

    I'm having a strange problem with the onclick attribute on a button not working in IE, it works fine in Firefox.

    Basically using Javascript I'm creating a button input and setting the onclick to call some othe Javascript. Yet when I click the button it does not call the function.

    Here's some code

    Code:
     	// create "remove me" button
     	var removeButton = createInput(null, 'Schedule', 'button', removeText);
     	
     	setElementAttribute(removeButton, 'onclick', 'removeBlock(\'' + topElement + '\',\'' + type + timeIndex + '\')');
     
     // the function createInput is defined as follows
     
     function createInput(id, className, type, value) {
     
     	var newInput = document.createElement('input');
     	setElementAttribute(newInput, 'id', id);
     	setElementAttribute(newInput, 'name', id);
     	setElementClass(newInput, className);
     	setElementAttribute(newInput, 'type', type);
     	setElementAttribute(newInput, 'value', value);
     	return newInput;
     }
    I can't figure this out, I tried this to see what the browser sees

    Code:
     	for (var i=0; i< removeButton.attributes.length; i++) {
     		if (removeButton.attributes[i].nodeName.toLowerCase() == 'onclick'){
     			alert(removeButton.attributes[i].nodeValue);
     		}
     	}

    The output from "alert" is what it should be, but it doesn't work.

    Thanks for your help

  2. #2
    SitePoint Enthusiast pentapenguin's Avatar
    Join Date
    Aug 2005
    Location
    GA, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a guess without seeing more of your code...
    Replace this:
    Code:
    setElementAttribute(removeButton, 'onclick', 'removeBlock(\'' + topElement + '\',\'' + type + timeIndex + '\')');
    with this:
    Code:
    setElementAttribute(removeButton, 'onclick', function(){
    removeBlock(topElement, type, timeIndex);
    }
    );

  3. #3
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I tried that but it doesn't work in Firefox or IE, also the function removeBlock takes only two parameters, here's the code for that:

    Code:
       function removeBlock(topElement, blockId, conf) {
       
       	if(conf || confirm('Are you sure you want to remove this class/office hour?')) {
       		var top = getObject(topElement);
       	
       		for (var i in top.childNodes) {
       			var nextTop = top.childNodes[i];
       			
       			if (nextTop != null) {
       				if (nextTop.id == blockId) {
       					top.removeChild(nextTop);
       				}
       			}
       		}
       	}	
       }
    I just realized I forgot to add the 3rd parameter, so I tried adding that to the previous script and it still fails:

    Code:
      	setElementAttribute(removeButton, 'onclick', 'removeBlock(\'' + topElement + '\',\'' + type + timeIndex + '\', false)');
    I'm not sure what other code you would need to see, I'd be happy to post more if you want.

    Thanks

  4. #4
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I downloaded the IE dom inspector, the page I am working with will parse an XML file and create a few of these block objects that I'm trying to remove, the ones that get created by PHP work fine, but the ones that are created by javascript don't, yet in the dom inspector they are identical. I'm really at a loss here.

  5. #5
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok after much googling I found the solution, in case anyone else runs into this problem, this fixed it:

    Code:
    setElementAttribute(removeButton, 'onclick', new Function('removeBlock(\'' + topElement + '\',\'' + type + timeIndex + '\', false)'));
    works in IE and Firefox


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •