webdevRefinery Forum: Change element background - webdevRefinery Forum

Jump to content

  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 09:47 AM (#1)

Change element background


Javascript div helper
I tried changing a div's background with this:

function change() {
    var bgimg = document.getElementById(bgimage);

bgimg.style.backgroundImage = "url('http://imgurl') repeat"
};

change();


I'm typing this on my phone so there may be a missing ; or so.
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline Cocoa 

  • Group: Members
  • Posts: 418
  • Joined: 30-November 10
  • LocationEngland

Posted 09 April 2012 - 09:52 AM (#2)

Why don't you just use CSS? But looking at the code, this line doesn't seem to be correct but I'm no JS expert. But the image won't be found with the following code because that's not a valid URL.
Dan || HTML || CSS || Web & Graphic Designer

Oh, there's no place like 127.0.0.1


Portfolio | Forrst (6 Invites) | Dribbble

MY FIRST TUTORIAL - HOW TO CREATE THE IRON MAN LOGO
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 10:24 AM (#3)

View PostCocoa, on 09 April 2012 - 09:52 AM, said:

Why don't you just use CSS? But looking at the code, this line doesn't seem to be correct but I'm no JS expert. But the image won't be found with the following code because that's not a valid URL.

Really? It's not a valid URL (sarcasm). The js is needed. Can't use CSS for this.
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline Qasim 

  • Group: Members
  • Posts: 520
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 09 April 2012 - 10:36 AM (#4)

Try this:
function change() {
    var bgimg = document.getElementById('bgimage');
    bgimg.style.backgroundImage = "url('http://imgurl') repeat";
}

change();


You forgot quotes around your id when populating the bgimg variable. Secondly, verify that your div certainly has "id='bgimage'" as well. Also might want to verify if your image path exists.
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 10:55 AM (#5)

Basically I downloaded greesemonkey and was trying things out but couldnt change a div background.

Edit it didn't work
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline Renegade 

  • 418 I'm a teapot
  • Group: Members
  • Posts: 748
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL,Graphics

Posted 09 April 2012 - 10:59 AM (#6)

Your CSS that you're trying to apply to the
div
is the part that's wrong. Basically what you're doing, in CSS, is this:

div {
    background-image: url(img) repeat;
}


That is incorrect. You can only specify the background image when using the
background-image
property. You were specifying the image and the repeating behavior. This is invalid CSS.

Your CSS code should be:

div {
    background-image: url(img);
    background-repeat: repeat;
}


And therefore your Javascript:

var div = document.getElementById("div-id");
div.style.backgroundImage = "url(img)";
div.style.backgroundRepeat = "repeat";


You can alternatively use the shorthand syntax but that should solve your problem.
http://adriancooney.ieGithubTwitterDribbbleForrst
We all die. The goal isn't to live forever. The goal is to create something that will.

Array(16).join({}-{}) + " Batman!";
1


User is offline Qasim 

  • Group: Members
  • Posts: 520
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 09 April 2012 - 11:16 AM (#7)

Thanks, Renegade. For some reason, I was thinking it was fine to use shorthands on the background-image. Would it be possible to just use
div.style.background = "url(img) repeat";
instead of specifying "backgroundImage"?
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is offline arronhunt 

  • I'm a httpster
  • Group: Moderators
  • Posts: 3398
  • Joined: 09-March 10
  • LocationLos Angeles, CA
  • Expertise:HTML,CSS,Javascript,Graphics,Flash

Posted 09 April 2012 - 11:18 AM (#8)

You could...

Renegade beat me to it.
DO NOT OPEN THIS

Spoiler
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 07:08 PM (#9)

Thanks guys it worked, turns out the issue was I had a user selected background :P. Check it out,
Posted Image

Anyways cools,
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 09:34 PM (#10)

New question why does
getElementsByClassName
Not get the various divs when getElementById does.
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline Qasim 

  • Group: Members
  • Posts: 520
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 09 April 2012 - 10:00 PM (#11)

How are you using getElementsByClassName? It will return an array, so to get a single dom object from it, you must do something like
getElementsByClassName('awesome')[0]
or whatever array number.
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 09 April 2012 - 10:02 PM (#12)

View PostQasim, on 09 April 2012 - 10:00 PM, said:

How are you using getElementsByClassName? It will return an array, so to get a single dom object from it, you must do something like
getElementsByClassName('awesome')[0]
or whatever array number.

Wow the article I read totally left out that it returns an array, there goes 15mins of wasted time. >:(
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 10 April 2012 - 02:23 AM (#13)

It doesn't return an array, it returns a NodeList, which pretends to be an array but updates whenever you change anything:

var cakes = document.getElementsByClassName('cake');
cakes.length; // 3

document.append(newCake);

cakes.length; // 4

Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 10 April 2012 - 07:06 AM (#14)

View Postcallumacrae, on 10 April 2012 - 02:23 AM, said:

It doesn't return an array, it returns a NodeList, which pretends to be an array but updates whenever you change anything:

var cakes = document.getElementsByClassName('cake');
cakes.length; // 3

document.append(newCake);

cakes.length; // 4


So can I do something like

cats = document.getElementByClassName('class');

cats.style.backgroundColor = "#ffffff";

ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 10 April 2012 - 07:10 AM (#15)

View Postitom07, on 10 April 2012 - 07:06 AM, said:

So can I do something like

cats = document.getElementByClassName('class');

cats.style.backgroundColor = "#ffffff";


Nope, that function doesn't exist. You want something like this:

var cats = document.getElementsByClassName('class');

cats[0].style.backgroundColor = "#ffffff";

Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 10 April 2012 - 07:38 AM (#16)

What If I want like the first five of the classes
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 10 April 2012 - 08:06 AM (#17)

View Postitom07, on 10 April 2012 - 07:38 AM, said:

What If I want like the first five of the classes

var cats = document.getElementsByClassName('class');

for (var i = 0; i < 5; i++) {
    cats[i].style.backgroundColor = "#ffffff";
}


:-/
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is online @Tom 

  • space
  • Group: Members
  • Posts: 704
  • Joined: 24-May 11
  • Locationspace
  • Expertise:Python

Posted 10 April 2012 - 08:31 AM (#18)

I thought it would be more complicated than a loop.
ocelotapps.com
jr wdR comedian under ThatRailsGuy

View Postarronhunt, on 30 June 2012 - 10:09 PM, said:

Sir you are the first person to make me piss myself laughing. Kudos.
0


User is offline callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 10 April 2012 - 08:36 AM (#19)

no
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline Renegade 

  • 418 I'm a teapot
  • Group: Members
  • Posts: 748
  • Joined: 08-March 10
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL,Graphics

Posted 10 April 2012 - 09:43 AM (#20)

View Postcallumacrae, on 10 April 2012 - 08:06 AM, said:

var cats = document.getElementsByClassName('class');

for (var i = 0; i < 5; i++) {
    cats[i].style.backgroundColor = "#ffffff";
}


:-/


Array.prototype.forEach.call(document.getElementsByClassName("class"), function(v, i) {
    if(i < 5) v.style.backgroundColor = "#f30e24";
});


:>
http://adriancooney.ieGithubTwitterDribbbleForrst
We all die. The goal isn't to live forever. The goal is to create something that will.

Array(16).join({}-{}) + " Batman!";
0


Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services