3

I am trying to turn a <div> into a link to local HTML document (./lilo/index.html) using JavaScript.

HTML

<div class="pagelist_item" onClick="goto("./lilo")">
    <h4>Test Button</h4>
    <h6>Discription</h6>
</div>

JavaScript

function goto(url){
    window.location = url;
    alert(url);
}

See http://jsfiddle.net/6HHTd/

But when I click the button, nothing happens.

Why does this not work?

0

2 Answers 2

7

Your quotes are incorrect in this line:

<div class="pagelist_item" onClick="goto("./lilo")">

jsfiddle even shows the error in red text.

Using apostrophes makes it easier to fix:

<div class="pagelist_item" onClick="goto('./lilo')">

To clarify, in "hi "there" you" the second double-quote matches with the first, closing the string and causing an error with the rest of the expression. Escaping the quotes with back-slashes works "hi \"there\" you" but embedding apostrophes (single-quotes) within double-quotes is often easier. (JavaScript is happy to use either single or double-quotes to delimit strings.)

Also rename your function from goto, as it is a reserved keyword.

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

4 Comments

As the question was "why does my JavaScript not work" then I would suggest that it is an answer.
@harsha yes it is an answer.
@DobbyIsDead: to clarify andy's response, you can't use goto as a function name, it is a keyword reserved for future use in javascript which cannot be used as an identifier.
Thank you @JoeMinichino. Someone else mentioned that it was a keyword but their comment disappeared. I've edited my answer to clarify.
2

Use jquery as follows

$('.pagelist_item').click(function(){
     window.location="./lilo";
});

Fiddle

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.