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
• 2,540 points
45 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,280 points
977 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
• 2,540 points
27 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
314 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,072 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,162 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,900 points
434 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,900 points
1,041 views
+9 votes
16 answers

Unable to resolve this error: "javac is not recognized as an internal or external command"

Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,620 points
380,963 views
0 votes
1 answer

How to resolve the error: could not find or load main class?

If you are getting error: could not ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,630 points
5,230 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP