dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Changing backgroun-image dinamically

  1. #1
    Join Date
    Jul 2012
    Location
    Barcelona
    Posts
    6

    Changing backgroun-image dinamically

    I want to change the background-image from javascript

    I get the current one by

    att = getComputedStyle(obj,'').getPropertyValue('background-image');

    the name of the image has a number in it. I replace just the number by

    var newatt = att.replace(new RegExp( "([0-9])", "gi" ),counter);

    alert(newatt); gives me the correct new name

    but, now

    getComputedStyle(obj,'').setProperty('background-image', newatt, 0);

    results in nothing to change.....

    may I get some help, please?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,439
    Are you sure you are using JavaScript and not Java?

  3. #3
    Join Date
    Jun 2009
    Location
    Miami
    Posts
    118
    The getComputedStyle() method returns an object of type CSSStyleDeclaration.

    While that object does have the .setProperty() method, the .getComputedStyle() method returns only a read-only interface to that object, according to Mozilla. Therefore only "get" (read) methods work, not "set" (write) methods.

    I suggest you re-code and try to open the CSSStyleDeclaration object with its read-write interface. Look at the internal and external stylesheets using Javascript, and find your property with information you obtained by .getComputedStyle(), then set it via the r/w interface.
    Last edited by mavigozler; 07-16-2012 at 03:24 AM.

  4. #4
    Join Date
    Jul 2012
    Location
    Barcelona
    Posts
    6
    thanks. Looks too complicated for me.

    I used jQuery instead and just one line of code made the job!

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