0

I need to run onclick function when <a href..></a> is clicked.

<div class="gallery">
    <div id="btn">
        <a href="#" id="btnRight" class="right"></a>
    </div>
</div>

<script type="text/javascript">
var element = document.querySelectorAll("a#btnRight.right");   
element.onclick = (function() {

    var count = 0;

    return function(e) {

        count++;
        console.log(count);
        if (count === 3) {

            //...

            count = 0;
        }
    };
})();
</script>

I can't use <a href="#" onclick=""...></a> in html so I need to select a#btnRight.right. I tried:

  • var element = document.querySelectorAll("a#btnRight.right");
  • var element = document.querySelector("#btnRight.right");
  • var element = document.getElementById("btnRight").getElementsByClassName("right")[0];
  • var element = document.getElementById("btnRight").getElementsByTagName("a");

But it doesn't work. Thank you

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
Hanz
  • 11
  • 1
  • 2
  • `querySelectorAll` will return a `NodeList`. Try `querySelector` instead. Also when you already have id why are you complicating selector? Just use `document.querySelector("@btnRight")` – Rajesh Feb 21 '17 at 12:09
  • 1
    Change to using `document.getElementById('btnRight')`, you don't need anything else, as an ID is always unique. – evolutionxbox Feb 21 '17 at 12:09
  • @evolutionxbox If your version works, then the posted version `var element = document.querySelector("#btnRight.right");` should also work – Andreas Feb 21 '17 at 12:11
  • 2
    http://jsbin.com/hejocar/1/edit?html,console,output — One of the versions you said doesn't work **does**. – Quentin Feb 21 '17 at 12:13

4 Answers4

1
<div class="gallery">
    <div id="btn">
        <a href="#" id="btnRight" class="right">CLICK ME!</a>
    </div>
</div>

<script type="text/javascript">
    var element = document.getElementById("btnRight");
    element.addEventListener('click', function() {
        var count = 0;
        return function(e) {
            count++;
            console.log(count);
            if (count === 3) {
                //...
                count = 0;
            }
        };
    }()
    )
</script>
  • What is the difference between `element.onclick` and `element.addEventListener(..)`? – Hanz Feb 21 '17 at 12:22
  • @Hanz [link](http://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick) BTW: your var element = document.getElementById("btnRight") returns array of elements, not element. And element.onclick = (function() {...})() runs when the document is loaded. you should not call this func, it will be called when element is clicked =) – Jmen Demidchenko Feb 21 '17 at 12:46
0

querySelectorAll returns a nodeList with all of the element with the specified id , so you need to change element to element[0] in case you have only one element with that id , and change your element id to btnRight like that :

<div class="gallery">
    <div id="btn">
        <a href="#" id="btnRight" class="right"></a>
    </div>
</div>

<script type="text/javascript">
var element = document.querySelectorAll("#btnRight");   
element[0].onclick = (function() {

    var count = 0;

    return function(e) {

        count++;
        console.log(count);
        if (count === 3) {

            //...

            count = 0;
        }
    };
})();
</script>

of course you can also use getElementById like that :

var element = document.getElementById("btnRight");   
element.onclick =  (function() {

    var count = 0;

    return function(e) {

        count++;
        console.log(count);
        if (count === 3) {

            //...

            count = 0;
        }
    };
})();
Developer
  • 460
  • 4
  • 17
0

little modification to your code:

<div class="gallery">
<div id="btn">
    <a href="#" id="btnRight" class="right">pp</a>
</div>

   document.getElementById("btnRight").onclick = CustomFunction;

   function CustomFunction() {

    var count = 0;

    return function (e) {

        count++;
        console.log(count);
        if (count === 3) {

            //...

            count = 0;

        }
    };
}

Sooraz
  • 113
  • 7
-1

Try below selector, you can select any DOM element using its ID and make sure ID should be unique in your DOM.

 element = document.getElementById('btnRight');

Also, If you are trying to select multiple elements having your 'right' class then you should use below selector.

 element = document.getElementsByClassName("right");
JoshulSharma
  • 163
  • 7