views:

124

answers:

4

I'm a coding a JavaScript reporting component, that requires multiple LI's i.e. lists to be selected collectively as a bunch with visual feedback.

I'm thinking of adapting the onfocus event. Is it possible for multiple HTML elements to receive focus at the same time?

Not inputs, but DIVs, so I don't need the cursor. I just want several DIVs to be "selected" separately from others, colored differently to simulate multiple item selection.

+3  A: 

No, you can only focus on one element at a time.

Babiker
Nice edit, probably saved you ;-)
Lauri Lehtinen
Yeah i had to edit it before any other answers were posted that way no suspicions of plagiarism will arise ;)
Babiker
+1  A: 

I don't believe so. If two text fields had focus at the same time, which would receive the input? You can "simulate" this (one field has focus and code "duplicates" the values), but only one item at a time can be "the focus".

liquidleaf
+2  A: 

No. The whole point of focus is that one element is selected(as in a "spotlight" for it). But if you want double writing text boxes then use this

<input type="text" name="firstbox" onchange="firstbox.value = secondbox.value; return true;">
<input type="text" name="secondbox">
Neb
+2  A: 

As the other answers state, only 1 element can have focus at any given time. What you could do instead is add a class to each of the 'selected' elements.

A simple example (using yui) would be:

 <style type="text/css">
     .selectedItem{border: 2px dashed #c0ffee;}
 </style>
...
<ul class='listContainer'>
     <li> ... </li>
     <li> ... </li>
     <li> ... </li>
</ul>
...
<script type="text/javascript">
     Y.one('.listContainer').delegate(
         'click', 
         function(e){ e.currentTarget.toggleClass('selectedItem');}, 
         'li'
     );
</script>
unomi
Demo at http://jsbin.com/axuce3/8 ;)
unomi