Clickable span not calling javascript even though w3school example works

0 votes

I have a clickable span element that is supposed to call a javascript function that toggles a dropdown. This is an amalgum of two w3schools examples linked here.

onclick with span element

clickable dropdowns with w3.css

My code is below, the HTML and JS are inline in the same HTML document. The CSS can be ignored, it is just renamed w3.css stuff (w3- to mxa-).

HTML

    <div class="mxa-dropdown-click">
        <span onclick="menu_click("page-menu")">
            <span class="mxa-xlarge">&#9776;</span>
        </span>
        <div id="page-menu" 
            class="mxa-dropdown-content mxa-bar-block mxa-border" 

            >

                <a href="/entity/show/42" class="mxa-bar-item">
                    Show Entity
                </a>

                <a href="/record/add/prompt/42" class="mxa-bar-item">
                    Add Record
                </a>

        </div>
    </div>

JS

    function menu_click(menu_id) {
        window.alert('i got to here');
        var menu = document.getElementById(menu_id);
        if (menu.className.indexOf("mxa-show") == -1) {
            menu.className += "mxa-show";
        } else { 
            menu.className = menu.className.replace("mxa-show", "");
        }
    }

I have edited an example from the w3schools site that to me looks essentially identical to my code but which does work.

<!DOCTYPE html>
<html>
<body>

<span id="demo" onclick="myFunction('demo')">Click me to change my text color.</span>

<script>
function myFunction(arg) {
    window.alert('i got to here');
  document.getElementById(arg).style.color = "blue";
}
</script>

</body>
</html>

I never 'get to here' in my code. What could I be doing wrong?

Apr 13 in Java by Rahul
• 3,380 points
1,401 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
1 answer

Javascript require() function giving ReferenceError: require is not defined

RequireJS is a JavaScript file and module ...READ MORE

answered Feb 17 in Java by Aditya
• 7,660 points
3,023 views
0 votes
0 answers

What is the !! (not not) operator in JavaScript?

I saw some code that seems to ...READ MORE

Apr 13 in Java by Rahul
• 3,380 points
74 views
0 votes
0 answers

Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?

I'm attempting to provide authorisation using JavaScript by connecting to the RESTful API integrated into Flask.  However, when I make the request, I receive the following error: XMLHttpRequest cannot load http://myApiUrl/login. No 'Access-Control-Allow-Origin' header ...READ MORE

Sep 20 in Java by Nicholas
• 7,120 points
48 views
0 votes
0 answers

Javascript require() function giving ReferenceError: require is not defined

Essentially, I'm using javascript to scrape data from the Google Play store by: 1-Request 2-Cheerios 3-QueryString I used Google Market API from Github ...READ MORE

Sep 21 in Java by Nicholas
• 7,120 points
90 views
0 votes
0 answers

What is the !! (not not) operator in JavaScript?

I encountered some code that appears to ...READ MORE

Sep 28 in Java by Nicholas
• 7,120 points
37 views
0 votes
1 answer

Calculate string value in javascript, not using eval

You could use the Function() constructor: function evil(fn) ...READ MORE

answered Nov 15 in Java by Damonlang
• 300 points
72 views
0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
407 views
0 votes
2 answers

“Could not find or load main class” mean?

Use the final modifier to enforce good initialization. Avoid returning ...READ MORE

answered Sep 18, 2018 in Java by Sushmita
• 6,900 points
3,316 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,900 points
2,510 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP