0

All I want to do is use this code: jsfiddle for an un-clickable button but I cant seem to embed the JS the way that i'm used to. I feel my problem is telling it what the img is. this is what i have.

<html>
<head>
    <script>
    jQuery(function($) {
    $('#img').mouseover(function() {
        var dWidth = $(document).width() - 100, // 100 = image width
            dHeight = $(document).height() - 100, // 100 = image height
            nextX = Math.floor(Math.random() * dWidth),
            nextY = Math.floor(Math.random() * dHeight);
        $(this).animate({ left: nextX + 'px', top: nextY + 'px' });
   </script>
</head>
<body>
    <div class="top">
        <p>Yada yada yada.<p>
    </div>
        <img src="poop.png" width="100" height="100" alt="Grey Square" id="img" />
</body>
</html>
6
  • 6
    What does this have to do with Java? Commented Apr 25, 2014 at 0:52
  • is this not java? I thoguht it was java... As it stands Im only literate in HTML and CSS. Commented Apr 25, 2014 at 0:54
  • You tagged it as both javascript and java — but Java and Javascript are almost completely unrelated. Commented Apr 25, 2014 at 0:56
  • 9
    @newbyJP "Java is to JavaScript such as Car is to Carpet" ;) Commented Apr 25, 2014 at 0:57
  • my apologize, I had no idea there was a different, but that is a great simile explanation. Commented Apr 25, 2014 at 1:03

2 Answers 2

2

You seem to be missing a few closing brackets and the part that loads jQuery itself:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script>
$(function() {
    $('#img').mouseover(function() {
        var dWidth = $(document).width() - 100, // 100 = image width
            dHeight = $(document).height() - 100, // 100 = image height
            nextX = Math.floor(Math.random() * dWidth),
            nextY = Math.floor(Math.random() * dHeight);
        $(this).animate({ left: nextX + 'px', top: nextY + 'px' });
    });   /// HERE
});   /// AND HERE
</script>

I suggest before learning jQuery, you should learn Javascript — surprisingly, and contrary to popular belief, those two are actually not the same thing!

Also, you need to pay attention to the error console of the browser; if you did, you'd have noticed the syntax error caused by the missing closing brackets.

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

8 Comments

Your right i did remove those on accident in other failed attempts, but this did not make my image move.
sorry; I actually also missed one bracket — edited; try again.
ReferenceError: jQuery is not defined calc.html:23 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.
OK, I realized you weren't even loading jQuery itself to begin with... see my updated answer.
I know i'm not suposed to say thanks based on the thing but... thanks a bunch you guys were a bunch of life savers.
|
1

You're using a JavaScript library called jQuery. This is a resource you need to include in your page. before </body>, write:
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
Explore this for more info.

6 Comments

i had high hopes for this one, but it still sits stagnently on the page. :{
I think the point of the jQuery(...) call is exactly to have the code executed after the page has loaded.
Oh yeah, I missed that because I'm not used to that signature. Anyway, @newby, did you include jQuery in your code?
@climbinghobo I posted the entirety of my page without all the added visible text. i'm not sure what you mean by "did i included..." but my gut says very strongly "i think??"
You're using a JavaScript library called jQuery. This is a resource you need to include in your page. before </body>, write: <script src="https://code.jquery.com/jquery-1.11.0.min.js"></script> Explore this for more info.
|

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.