XUI toggleClass error

445 Views Asked by At

I have a input that I press then this event is called:

document.getElementById("nearest").addEventListener('click', function(){
    x$("#popup").toggleClass('hide');
}, false);

then I get this error in console:

x$("#popup").toggleClass is not a function
[Break On This Error] x$("#popup").toggleClass('hide'); 

This is the div and the input element

<div id="popup" class="hide"></div>
<input id="nearest" type="image" name="nearest">
1

There are 1 best solutions below

0
On

The below code seems to work as expected—it toggles the background color from red to blue (tested in FireFox 8.0 and Chrome 17.0.942.0):

<html>
<head>
    <style type="text/css">
        #popup{
            position:relative;
            height:100px;
            width:100px;
            margin:10px;
            background-color:red;
       }
        .hide{
            background-color:blue !important;
        }
    </style>


    <script type="application/javascript" src="http://xuijs.com/downloads/xui-2.3.2.min.js"></script>
    <script type="application/javascript">
        x$.ready(function() { 
            document.getElementById('nearest').addEventListener('click', function(){
                x$("#popup").toggleClass('hide');
            }, false);
        });
    </script> 
</head>
<body>
    <div id="popup" class="hide"></div>
    <input id="nearest" type="image" name="nearest" />
</body>
</html>

Just to confirm, I assume the document.getElementId() you originally used was part of a function or enclosed in some onLoad script? In the working example I used XUIs ready() function to make sure the DOM had loaded.

It's also worth noting that the below code is equivalent, since you're using XUI anyway:

x$.ready(function() { 
    x$('#nearest').click(function(){
        x$("#popup").toggleClass('hide');
    });
});