Reputation: 1986
I am a beginner in jQuery programming. I am trying to fire a click event on list click, but it is not working properly. The following is my code:
<html>
<head>
<script type="text/javascript" >
<script type="text/javascript" src="jquery.js">
<link rel="stylesheet" href="style.css" />
$("ul#select li").click(function(e){
//targ = $(e.target);
if($(this).hasClass("selected"))
{
$(this).removeClass("selected");
document.location= "cambridge:clearanswer";
}
else
{
currentanswer = $(this).html();
document.location = "cambridge:checkanswer:"+currentanswer;
$("ul.select li").removeClass("selected");
$(this).addClass("selected");
}
});
function getUsed()
{
var total = "";
$(".boxWord.boxWordUsed").each(function (i){ total = total + this.getAttribute('index_no') + ":"; });
return total;
}
</script>
</script>
<link href="style.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="question" >
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" class="select" id="select">
<li data-role="list-divider" class="selected">Overview</li>
<li data-role="list-divider" class="selected">Overview</li>
</ul>
</div>
</body>
</html>
Upvotes: 0
Views: 4396
Reputation: 12503
First of all you are not writing codes in <script>
tag and the code messed up between multiple tags. Then you have use $(document).ready()
. You also have other errors in code, I have corrected all of them for you, I understand you are a newbie :).
See a working demo here http://jsfiddle.net/DtHJY/1/
Here is the corrected code:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
$(document).ready(function(){
$("ul#select li").click(function(e){
alert('test');
//targ = $(e.target);
if($(this).hasClass("selected"))
{
$(this).removeClass("selected");
document.location= "cambridge:clearanswer";
}
else
{
currentanswer = $(this).html();
document.location = "cambridge:checkanswer:"+currentanswer;
$("ul.select li").removeClass("selected");
$(this).addClass("selected");
}
});
function getUsed()
{
var total = "";
$(".boxWord.boxWordUsed").each(function (i){ total = total + this.getAttribute('index_no') + ":"; });
return total;
}
});
</script>
<link href="style.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="question" >
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" class="select" id="select">
<li data-role="list-divider" class="selected">Overview</li>
<li data-role="list-divider" class="selected">Overview</li>
</ul>
</div>
</body>
</html>
Upvotes: 2
Reputation: 50982
try
document.location.href = 'link'
instead
and also do not forgot to wrap your code into
$(function(){
//your code
});
Upvotes: 0
Reputation: 10077
As long as you have wrapped the event binder in $(document).ready(function() { ... });
then it will work perfectly.
Have a look at this demo: http://jsfiddle.net/E5BZ9/
Upvotes: 0
Reputation: 4533
You need to wrap your script up in document ready:
$(document).ready(function() {
...your click event code
});
Otherwise it may try to attach the event before the element is loaded.
Upvotes: 2