www.webdeveloper.com
Results 1 to 6 of 6

Thread: setAttribute works GREAT in FireFox - ie doesn't like it

  1. #1
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582

    setAttribute works GREAT in FireFox - ie doesn't like it

    I've been working on this for about four hours (two today, two yesterday) and keep hitting the brick wall.

    I'm trying to create a function that will add an attribute to specified tags. As the title says, it works great in FF and not at all in IE7.

    Probably the first question you'll ask after you see my code is "why?" Well, I've got a form with over 100 form fields in it, and if I can avoid manually adding an attribute to all of them, I'll take the JS route.

    Here is my code. Can anyone please offer a solution to get this to work with IE7?
    Code:
    function addEventToElement(formName,tagName,typeName,eventName,eventAction) { 
    		elem = new Array();
    	  thisForm = (typeof formName === 'string') ? document.getElementById(formName) : '' ;
    	   thisTag = (typeof tagName === 'string') ? tagName : '' ;
    	  thisType = (typeof typeName === 'string') ? typeName : '' ;
    	 thisEvent = (typeof eventName === 'string') ? eventName : '' ;
    	thisAction = (typeof eventAction === 'string') ? eventAction : '' ;
    	if(thisForm != "") { // If elements are in a form, make sure ONLY those elements are affected
    		k = 0;
    		elem = thisForm.getElementsByTagName(thisTag); // array of all items of [tag]
    		if(thisType != "") {
    			for(j=0;j<elem.length;j++) {
    				if(elem[j].type != thisType) { k++; elem.splice(j,1); } // If a type is specified, remove tags that do not have a type attribute
    				}
    			}
    		}
    	else { // Otherwise, any/all elements in a document/body will be affected
    		elem = document.getElementsByTagName(thisTag); // array of all items of [tag]
    		if(thisType != "") {
    			for(j=0;j<elem.length;j++) {
    				if(elem[j].type != thisType) { elem.splice(j,1); } // If a type is specified, remove tags that do not have a type attribute
    				}
    			}
    		}
    	if((thisEvent != "") && (thisAction != "")) {
    		for(i=0;i<elem.length;i++) { // All elements are picked - let's apply some attributes
    			thisForm[elem[i].name].setAttribute(thisEvent,thisAction); // This works well in FF - not at all in IE
    			}
    		}
    	}
    addEventToElement('form name goes here, if there is one - if not, put NULL','input','text','onkeydown','return numbersOnly(event,this);')

    FORMNAME, if there is one; TAGNAME; TYPE, if there is one; EVENT; ACTION/FUNCTION are the arguments.

    Assistance greatly appreciated.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  2. #2
    Join Date
    Dec 2010
    Posts
    207
    Never use setAttribute where direct assignment can be used, since the definition of what is an attribute may vary.

    Try
    Code:
    thisForm[elem[i].name][ thisEvent ] = thisAction;
    Where thisEvent is (as you have now) a string name of an event and thisAction is a function reference, not a string.


    Q: I found this code...
    A: Then find its author.

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    I've already tried that one. It's not giving an error message, but it's not working, either. It will not prevent letters from being entered in the fields (which is the function that it is calling.)

    If only IE had the awesome Error Console that FF has! Bah!



    (Actually, I just noticed that my function has a ' out of place. I'll give that another shot with the proper placement and let you know.)

    [Nope.. still isn't working.]
    Last edited by WolfShade; 05-10-2011 at 10:08 AM.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Waitaminnit.. if thisAction is supposed to be a function reference, not a string, how do I pass it arguments??
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  5. #5
    Join Date
    Dec 2005
    Posts
    2,984
    Event handlers will always, by default be passed the event object as an argument. You can't make them accept any others using the method where you assign a the function reference to the event

    e.g.
    Code:
    myelem.onclick = myFunc;
    You can of course use this method:

    Code:
    myelem.onclick = function(e) { myFunc(e,arg1,arg2,arg3); };
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WolfShade View Post
    Waitaminnit.. if thisAction is supposed to be a function reference, not a string, how do I pass it arguments??
    But it could be a string. In which case, you may use the fact that a global function is, in fact, a property of the window Global Object
    Code:
    <script type="text/javascript">
    function foo(id){
    alert(id)
    }
    onload=function(){
    var div=document.getElementById('mydiv');
    var thisEvent='onclick';
    var thisAction='foo';
    div[thisEvent]=function(){window[thisAction](this.id)}
    }
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles