tmanolescu tmanolescu - 5 months ago 29
Javascript Question

selecting anchor elements with specific class

I want to select the menu-enabled anchor with javascript, which is a small image and when I click it it should change a class of the background to highlight the menu.
The change class works, I tried document.onclick and it works, but when I try to be specific wrapper>nav> it doesn't select it.
I've tried:

var menu = document.getElementById('nav');
var anchor = menu.getElementsByTagName('a');

to be able to click on every link just to test, but it didn't work.

I've tried:

var anchor = menu.getElementsByClassName('menu-enabled');

and then

anchor.onclick...` but still no result :(

tried using jquery

$("").onclick = function()

but still no result

Currently is set to click anywhere on the document until I get it done.

document.onclick = function() {

<div id="wrapper">
<div class="logo">
<a href="index.html">Tro</a>
<nav id="nav" class="menu-icon">
<a class="menu-enabled">&nbsp;</a>
<a class="link-enabled" target="_blank">&nbsp;</a>
<a class="prev-disabled">&nbsp;</a>
<a class="next-disabled">&nbsp;</a>
<a class="back-disabled">&nbsp;</a>
<div class="overlay">
<nav class="meniu">
<a href="#1">Link 1</a>
<a href="#2">Link 2</a>
<a href="#3">Link 3/a>
<a href="#4">Link 4</a>

Edit 1: fiddle

Edit 2: Ok, I think the problem is I could not click the button, tried it on something else and it worked, I thinkg the z-index is the problem, gonna return with an answer soon.

Edit 3: it could be because I gave the anchor a background?{
background: url( no-repeat 0 0;
background-size: 24px 24px;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
opacity: 1;
visibility: visible;
z-index: 99;

Answer Source


    $(document).on('click', '', function(e){
        // Do something
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download