0

So, I've got a small website that I'm working on-- it's very simple, I'm just using it to EVENTUALLY play with C++ and Javascript COM interfaces and bitwise operators.

But I'm getting stuck on the simple stuff.

Here's my HTML, CSS, and jQuery code respectively (formatted for jsfiddle).

<title>Can you guess the combination?</title>
<body>
    <div>
        <label id="instructions">Guess the combination and you win!</label>
    </div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
</body>

.button {
    background-color:#0F0;
    width:3em;
    height:3em;
    display:table-cell;
    float:left;
    margin:0.5em;
}
#instructions {
    font-family:roboto;
    font-color:#bbbbb;
}
a {
    font-family:roboto;
}

$(document).ready(function () {
    $(".button").hover(

    function () {
        $(this).css("background", "#ddd");
    },

    function () {
        $(this).css("background", "#ccc");
    });
});

Here is a jsfiddle of what I have, where hovering over the boxes should cause them to change color. I can't get this example working. It's very simple, but I can't seem to tell what I've done.

Later I'll have more advanced features, but as it is I'm tired of staring at this myself.

Do you have any ideas, {StackOverflow Member}?

2
  • 5
    jsfiddle.net/nu62L/1 just include jquery Commented Dec 5, 2013 at 17:56
  • you had missed including a js library...possible a google hosted cdn Commented Dec 5, 2013 at 17:57

6 Answers 6

4

Use css for this

.button {background-color: #ddd;}
.button:hover {background-color: #ccc;}
Sign up to request clarification or add additional context in comments.

1 Comment

This is a solution, but the question was more about how to solve it using jQuery. Thank you, though!
2

Updated JSFiddle here.

The problems were that jQuery wasn't included in the jsFiddle, there was no reason to have a $('document').ready(), and the correct CSS is background-color, not background

Code here

$(".button").hover(
    function () {
        $(this).css("background-color", "#ddd");
    }, 
    function () {
        $(this).css("background-color", "#ccc");
    }
);

2 Comments

background is shorthand property if you wanna write e.g. background: #f5f5f5 url(../images/bg.png) it's correct & valid.
And $('document').ready() is necessary because the jQuery is above the <div>s and I want it to load afterward, so it can include the loaded <div>s.
1

You need to concentrate to top-left of JS Fiddle and change the value of the first drop down as below image:

JS Fiddle Screen Shot

2 Comments

This got it working on jsfiddle, which means my in-house solution is simply lacking jQuery interpretation. Now I need to make it work for Javascript.
Thanks to your answer, I was able to figure out the solution, which will be submitted and this turned into a community post.
1
This is working correctly 
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
    $(".button").hover(

    function () {
        $(this).css("background", "#ddd");
    },

    function () {
        $(this).css("background", "#ccc");
    });
});
</script>
<style>
.button {
    background-color:#0F0;
    width:3em;
    height:3em;
    display:table-cell;
    float:left;
    margin:0.5em;
}
#instructions {
    font-family:roboto;
    font-color:#bbbbb;
}
a {
    font-family:roboto;
}
</style>
</head>
<body>
    <div>
        <label id="instructions">Guess the combination and you win!</label>
    </div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
    <div class="button"></div>
</body>
</html>

1 Comment

Thanks to parts of your answer I was able to form my own. Please view my community wiki answer to see what my final solution was.
0

The correct CSS is color, not font-color.

To change background, use background-color.

1 Comment

That's not the question and this should be a comment :)
0

The solution turned out to be multi-fold.

First, I did not have jQuery included in the project. I was running a script written in jQuery but without pre-loading that which lets the Javascript interpreter handle jQuery.

To amend this, just above the <script> that includes my jQuery work, I included this:

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

Now the Javascript engine can handle jQuery commands.

Next, I was mis-labelling one of the CSS properties. Where in my jQuery script I had written:

$(document).ready(function(){
    $(".button").hover(
        function () {
            $(this).css("background", "#ddd");
        }, 
        function () {
            $(this).css("background", "#ccc");
        }
    );
});

I should have instead written:

$(document).ready(function(){
    $(".button").hover(
        function () {
            $(this).css("background-color", "#ddd");
        }, 
        function () {
            $(this).css("background-color", "#ccc");
        }
    );
});

(I had missed that background is not a CSS property, and what I meant was background-color)

A note when writing your own jQuery code: If your <script> tag is before your actual web code, make sure that the code inside the script is nested inside the line:

$(document).ready(x)

Because without that, the Javascript engine will execute operations on HTML objects that don't exist yet; With this line however, it will wait until all the HTML objects are loaded and modifiable.

Comments

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.