1

*New question following suggestions:

HTML head contains:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="myscript.js"></script>

Here is my HTML:

<div id="sidebar">
    <table id="table1">
        <tr>
            <th>Table</th>
        </tr>
        <tr>
            <td>
                <a href="javascript:;" rel="img1">Link1</a>
            </td>
            <td>
                <a a href="javascript:;" rel="img2">Link2</a>
            </td>            
        </tr>
    </table>
</div>

<div id="box">
    <img src="http://icons.iconarchive.com/icons/artdesigner/emoticons-2/256/cant-believe-it-icon.png" id="img1" class="images"/>
    <img src="http://icons.iconarchive.com/icons/artdesigner/emoticons-2/256/too-much-icon.png" id="img2" class="images"/>
</div>

And my CSS:

#sidebar {
    display: inline-block;
    height: auto;
    width: auto;
    font-family: Garamond;
    font-size: large;
}

#table1 {
    border: 1px solid black;
    text-align: center;
}

#table1 th {
    border: 1px solid black;
}

#table1 td {
    border: 1px solid black;
}

#box {
    position: relative;
    height: 200px;
    width: 1200px;
}

.images {
    display:none;
    position: absolute;
    top: 0px;
    left: 0px;
}

And my Javascript:

$('a').click(function(){
    imgid = $(this).attr('rel');
    $('a').removeClass('active');
    $(this).addClass('active');

    $('img').hide();    
    $('#'+imgid).fadeIn('slow');
});

This should mean that when the Link1 is clicked, the first image appears and when the Link 2 is clicked, the second image appears and the first goes away (the images are on top of each other in CSS). However, when either of the two are clicked, nothing happens. Any suggestions why this may be the case?

2
  • 4
    quirksmode.org/js/placejs.html Commented Apr 18, 2014 at 19:39
  • Update: After deleting the .images {display:none;} in the CSS, both images are showing, but the links still don't do anything - shouldn't the jQuery cause one image to hide when a <td> is clicked though? Commented Apr 19, 2014 at 13:05

2 Answers 2

1

You have two choices:

Save your javascript code into a file with .js extension code.js then import it to your html file using <script type="text/javascript" src="code.js"></script>

Or you can put your code directly in your HTML file in the Head part like this :

  <head>
    <script>
    // Your Javascript Code
    </script>
  </head>

And don't forget to import jQuery

  <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
Sign up to request clarification or add additional context in comments.

18 Comments

So I put copy the jQuery part directly into the <head>, then link to the javascript file in the head, changing the source? After doing that, and removing the href attributes, when I click on it, nothing happens. I have included the document ready function part as other comments have said too.
u can't use the two methods at the same time. either you make a .js file and import it. or you write the code directly in the header tag and between <script></script>. and the important thing is after choosing a method.you must to include the jQuerry.js (the two last lines) in your html file
Okay I'm seriously confused after never working with javascript or jQuery before, so let me try to explain exactly what I've done. I saved the second part of my code in my question as a .js file. Then I linked to that in the head (not writing the code between <script> tags. I'm then unsure about the whole jQuery part - is my code javascript or jQuery? and what do I need to do to amend that? I have removed the href attributes that were in my original question. I have added onclick="ChangeImage();" to the <a> tag. I have copied the coding with the document ready part to fix my javascript file.
And finally, I've added document.getElementById("clickMe").onclick = ChangeImage(); to the top of the .js file, and the first line from my original code (don't know if its javascript or jQuery) now reads $('a').click(function ChangeImage(){ so that the Change Image is the function to call.
The code works right. My Friend you need to import jquery-ui.js check this link
|
0

Of course at first you have to include your javascript file in the html, best practice include it in the header:

<script type="text/javascript" src="/destination/to/file.js"> </script>

then in the

<a href="#" onclick="FunctionToCall();">Link</a>

and make sure your javascript file is modified to get the element by its id to run the function properly.

6 Comments

What would be the function to call from my javascript file (sorry haven't got round to learning javascript so I only know the extreme basics).
Lets assume that your function is called, ChangeImage which is declared in javascript as: function ChangeImage() { //..... Rest of your code; } At the very beginning in your js file type: document.getElementById("clickMe").onclick = ChangeImage();
I would advise against declaring event handlers in HTML code. Use $(document).ready(function() { $("a").click(FunctionToCall()) }) instead.
Every time I see any kind of inline javascript, I kill a kitten. Stop hurting the kittens!
Should I get rid of the href attributes this way as mentioned in the other comment?
|

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.