2

here is what I've got and I know it isn't right. Basically, I'm trying to say, "If the height of the div called three is 445, change the background image to this jpg."

if ( ".three" == height:"445" ) {".three.style.background-image"= 'url(images/2.jpg)'};

Thanks in advance!

8
  • 5
    Oh, that's a mess... You should start from the very basics. Commented Apr 4, 2013 at 22:16
  • How many elements are there with that class? If it's just one, why not give it an ID? Then have a look at this: stackoverflow.com/q/10978557/218196. Commented Apr 4, 2013 at 22:16
  • 1
    Is that <div id="three"> or <div class="three">? Commented Apr 4, 2013 at 22:16
  • . is always a class, right? # refers to id Commented Apr 4, 2013 at 22:19
  • In CSS, yes. What's your (representative) HTML that you're trying to style with this conditional? Commented Apr 4, 2013 at 22:20

1 Answer 1

7

Assuming that your HTML is along the lines of:

<div class="three" style="height: 445px;">
    <p>Some arbitrary content.</p>
</div>

Then the following will work:

var elems = document.getElementsByClassName('three');
for (var i = 0, len = elems.length; i < len; i++){
    if (parseInt(elems[i].style.height, 10) == 445) {
        elems[i].style.backgroundImage = 'url(images/2.png)';
    }
}

JS Fiddle demo, using background-color instead of background-image for simplicity).

If, however, you're using CSS to style the elements:

.three {
    height: 445px;
}

Then you'd need to use window.getComputedStyle():

var elems = document.getElementsByClassName('three');
for (var i = 0, len = elems.length; i < len; i++){
    console.log(parseInt(window.getComputedStyle(elems[i], null).height, 10));
    if (parseInt(window.getComputedStyle(elems[i], null).height, 10) == 445) {
        elems[i].style.backgroundColor = 'red';
    }
}

JS Fiddle demo, (using, as above, background-color instead of background=image).

If you were to use a JavaScript library, then this could be simplified somewhat; with jQuery (for example, though I'm not especially advocating jQuery, it's just the library with which I'm most familiar), the above could be rewritten as:

$('.three').css('background-image', function(){
    return $(this).height() == 445 ? 'images/2.png' : '';
});

JS Fiddle demo, (again using background-color instead of background=image).

Note that Internet Explorer works differently to most browsers, in that window.getComputedStyle() isn't available, there is currentStyle(), however (but without Windows I can't offer advice on how to use it).

For guidance, and reference, on JavaScript I'd recommend (above almost all else) reading through the Mozilla Developer Network's JavaScript documentation.

References:

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that looks much more complicated.
Much more complicated? Maybe, but it works; which I assumed was the important thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.