Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript error Uncaught TypeError: $(...).onclick is not a function [closed]

I am beginner and I got an error on my JavaScript.

JavaScript:

$(function(){
    $(".widgetselecterbtn").onclick(function(){
        var index = $(".widgetselecterbtn").index(this);
        $('.itemselected').addClass('hide');
        $('.itemselected').eq(index).removeClass('hide');
    });
});

HTML:

<ul id="wighet">
    <li><div><i class="fa fa-header fa-lg"></i><br><a class="widgetselecterbtn">heading</a></div></li>
    <li><div><i class="fa fa-pencil fa-lg"></i><br><a  class="widgetselecterbtn">text</a></div></li>
    <li><div><i class="fa fa-youtube fa-lg"></i><br><a class="widgetselecterbtn">Youtube</a></div></li>
    <li><div><i class="fa fa-quote-left fa-lg"></i><br><a class="widgetselecterbtn">quate</a></div></li>
    <li><div><i class="fa fa-link fa-lg"></i><br><a class="widgetselecterbtn">link</a></div></li>
    <li><div><i class="fa fa-picture-o fa-lg"></i><br><a class="widgetselecterbtn">image</a></div></li>
    <li><div><i class="fa fa-twitter fa-lg"></i><br><a class="widgetselecterbtn">Twitter</a></div></li>
</ul>
<div class="ItemWidget01 itemselected hide">                 
</div>
<div class="ItemWidget01 itemselected hide">            
</div>
<div class="ItemWidget01 itemselected hide">            
</div>
<div class="ItemWidget01 itemselected hide">                 
</div>
<div class="ItemWidget01 itemselected hide">                
</div>
<div class="ItemWidget01 itemselected hide">                
</div>
<div class="ItemWidget01 itemselected hide">                  
</div>

The error is

Uncaught TypeError: $(...).onclick is not a function

How can I solve this?

like image 724
vanhiro Avatar asked Jan 26 '16 10:01

vanhiro


People also ask

Is not function error in JavaScript?

The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function.

Is not a function Typeerror is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


1 Answers

onclick is not a jQuery function. You should use click instead.

$(".widgetselecterbtn").click(function() {
    //do your task here
});
like image 139
Ibrahim Khan Avatar answered Oct 04 '22 03:10

Ibrahim Khan