Search code examples
javascripthtmlarea

How to browse area list in Javascript


I am trying to learn Javascript,

Can someone tell me if it is possible to browse an area List with Javascript ?

If yes how can I do it?

Here is my HTML Code:

 <map name="rx" id="rx">
    <area href="Link.gg" coords="137,92,25" shape="circle" alt="Blabla">
    <area href="Link1.gg" coords="227,93,25" shape="circle" alt="Blabla1">
    <area href="Link2.gg" coords="295,105,25" shape="circle" alt="Blabla2">
 </map>

What is the JS Code to browse this ?

If it is possible I have got one more question,

HTML Code:

<area href="Exemple.gg" coords="295,105,25" shape="circle" alt="Unicorn">

How can I get the value of the href and the alt ?

( `var myvar = "Exemple.gg"; var myalt = "Unicorn";`)

I just want to get the text from the href and alt.


Solution

  • const areas = document.querySelectorAll('area'); // use more precise selector if needed
    let myElement;
    
    areas.forEach(el => {
        if (!myElement && el.getAttribute('alt') === 'Red') {
           myElement = el;
        }
    });
    
    console.log(myElement)