0

I have wrote this code on my local server (Installed Apache 2 on my Mac with MacPorts) but It haven't run.

JavasSript is active on Safari or Firefox, but it haven't do on these. Is this code worse? Or I can try other way? Please help.

<html>
<head>
  <title> jquery test </title>
   <script src="http://www.google.com/jsapi"></script>
   <script>
     // Load jQuery
     google.load("jquery", "1");
   </script>

   <script type="text/javascript">
    JQuery(function($){
     var $curr = $(".sel");
     $("button").click(function(){
      $curr.removeClass("sel");
      $curr.$curr.prev().addClass("sel") 
     });
    });
   </script>
   <style type="text/css">
   span { padding :8px;}
   .sel { border :orange solid 4px;}
   </style>
      </head>
      <body> 
   <p>
    <span>1</span>
    <span>2</span>
    <span class="sel">3</span>
    <span>4</span>
    <span>5</span>
    <button>click</button>
   </p>
</body>
</html>

2 Answers 2

4

It should be jQuery instead of JQuery, you'll get a JavaScript error calling a variable that doesn't exist :)

Also this has an extra $curr:

$curr.$curr.prev().addClass("sel") 

It should just be:

$curr.prev().addClass("sel") 

You can see a version with both of these fixes here


If you always want to move back one, you need to move your selector inside the click, instead of always referring to the original element that had class="sel", like this:

jQuery(function($){
  $("button").click(function(){
    $(".sel").removeClass("sel").prev().addClass("sel");
  });
});​

You can test it here

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

1 Comment

Thank you for your advice! It works! I mistook "jQuery" spell. And jsfiddle is cool.
0

To help you debug, both safari and firefox has javascript error/output logs. On firefox press control shift j to open up the console, here you can see what went wrong.

1 Comment

thank you for your help. I want also cool debuging way with js, but I didn't it. awasome!

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.