www.webdeveloper.com
Results 1 to 4 of 4

Thread: building a function to change the style of an array of elements

  1. #1
    Join Date
    Mar 2008
    Location
    NJ, USA
    Posts
    18

    building a function to change the style of an array of elements

    I am working on a function that will take an array of elements and loop thru them to change a particular style property. The current function is as follows

    Code:
    function style(prop,val) {
    	if (els == null) {//els is the array of elements to be changed
    		err('missing elements');
    		return false;
    	}
    	for(i in els) {
    		var x = els[i].style;
    		x.prop = val;//This is where my problem is.  Is there a way to do this?
    	}
    }
    Thanks in advance,
    -Sad1sm0

  2. #2
    Join Date
    Jul 2007
    Location
    Cracow
    Posts
    78
    Maybe eval function will help:
    Code:
    eval("x." + prop + " = '" + val + "';");

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Nope. eval() is to be avoided whenever is possible (and most of the time it is)
    Use square brackets notation:
    Code:
    for(i in els) {
    els[i].style[prop]=val;
    }

  4. #4
    Join Date
    Mar 2008
    Location
    NJ, USA
    Posts
    18
    Quote Originally Posted by Kor
    Nope. eval() is to be avoided whenever is possible (and most of the time it is)
    Use square brackets notation:
    Code:
    for(i in els) {
    els[i].style[prop]=val;
    }
    Thanks KOR... works like a charm

Thread Information

Users Browsing this Thread

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

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