1

I have a page which has got many div under anchor tag.

<a>
    <div onclick=alert("Hi First div");>First</div>
</a>
<a>
    <div onclick=alert("Hi Second div");>Second</div>
</a>.......

I have onclick event on every div which is working fine as i click on the div.

Is there a way i can traverse every div using tab (with/without using tabindex)and on click of enter key on keyboard the alert is appeared without writing any JS?

I am only trying to acheive this for accessibility.

2 Answers2

0

What you are trying to do is to set tabindex behaviour on elements that do not support it.

There is an explanation in this other question.

Look at this question. Tab Index on div

Community
  • 1
  • 1
Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
  • I miss the part where images are getting mentioned. Care to elaborate? – Lain Nov 09 '16 at 13:05
  • Ah ok - just reread the question and I see that there is no mention of images. I will edit out that part of the answer. Actually I think your answer @Lain is best as the OP can style the anchors like divs anyway. Well done I will upvote yours. – Vanquished Wombat Nov 09 '16 at 13:13
0

In your case, I can not see any reason to not make use of the anchor tags built in behavior:

<a href = '#' onclick = 'alert("Hi First div");' tabindex = '1'>First</a>
<a href = '#' onclick = 'alert("Hi Second div");' tabindex = '2'>Second</a>
Lain
  • 3,657
  • 1
  • 20
  • 27