1

I want to allow the use to slide up and down when pressing up arrow and down arrow, I mean if "roma" is highlighted and I pressed down then "milan" should be highlighted. Here is my HTML:

<li id="place">
  <p>roma</p>
  <p>milan</p>
  <p>napoli</p>
</li>

I want to do this using Javascript, perhaps using onclick(). which javascript attribute should i used and how to hightlight something using css thank you

William Kinaan
  • 28,059
  • 20
  • 85
  • 118

1 Answers1

1

You can append a CSS class to highlight the respective element upon clicking it. Using jQuery toggle will allow you to toggle the highlighting of an element. jsFiddle example coming soon...

jQuery Example: http://jsfiddle.net/2NS5V/2/

JavaScript Example: http://jsfiddle.net/GDEeW/1/

Jack
  • 15,614
  • 19
  • 67
  • 92
  • 2
    Remember he said **javascript**, *not* **jquery** :) – Mathew Thompson May 08 '12 at 21:26
  • 2
    yeah :) I'm creating an example for both. – Jack May 08 '12 at 21:30
  • i really thank you , but i don't know jQuery in css how can i hightlight something ? – William Kinaan May 08 '12 at 21:31
  • 1
    you can highlight simply by changing the CSS background-color – Jack May 08 '12 at 21:32
  • good , and how can i make the highlight remove from p tag to another p tag , like my exapmple from roma to milan , should i used javascipt ? – William Kinaan May 08 '12 at 21:33
  • 1
    Yeah, you should use JavaScript. – Jack May 08 '12 at 21:36
  • what is the method to call it to listen for pressing up arrow and down arrow ? thank you – William Kinaan May 08 '12 at 21:49
  • really thank you man for the example , i really thank you , hope u a good life :) – William Kinaan May 08 '12 at 21:51
  • i saw the first example , and i will see javascript one , i really thaaaaaaaaaaaaaaaaaank you – William Kinaan May 08 '12 at 22:05
  • the last thing , how can i tell java script to do that when the using click the up and down arrow , not a prev and next button ? – William Kinaan May 08 '12 at 22:06
  • 1
    Up and Down keyboard arrows? Read this: http://stackoverflow.com/questions/5597060/detecting-arrow-keys-in-javascript – Jack May 08 '12 at 22:10
  • i understood ur javascript code , but if i want to use it in a function have i to call that function when displaying the page , ? something like onload ? – William Kinaan May 08 '12 at 22:14
  • Whilst I can't dispute that your code works, this is a god awful solution to the problem. You have hard-coded the number of options in the list, and what is worse, you have hard-coded how to highlight each of the possible options, what would happen if this was a list of over 10,000 options? Would you really write a switch case fro each of the possible options? – thecoshman Jul 24 '12 at 08:00