Baldráni
Baldráni

Reputation: 5640

Unable to get an element in jQuery

I'm trying to do a custom select but I get stuck when I want to change the text of what is displaying. Probably an idiot problem or just me being blind but I don't get why is it returning me this.

Have a look a this fiddle : -> Here

Uncaught TypeError: Cannot read property 'click' of null(…)

But actually the thing I am clicking on is completely define since it is a li element. I tried to target it by id or class but I have the same error, tried to use each, style the same problem I clearly don't know anymore where this is coming from.

This is the code I'm using.

$('.custom-select').click(function(){
    $('.customSelect').toggle();
});
$(document).on('click','li',function(){
    $(".custom-select").text() = $(this).text();
});

I you guys have any clue.

Upvotes: 0

Views: 29

Answers (1)

JTheDev
JTheDev

Reputation: 499

 $(document).ready(function () {
     $('.custom-select').click(function () {
        $('.customSelect').toggle();
    });
    $(document).on('click', 'li', function () {
        $(".custom-select").text($(this).text());
    });
});

Please take a look at this and see.

If you want to hide the list after selecting, then add the below code at the end of $(document).on(function(){

$('.customSelect').toggle();

JSFiddle

Upvotes: 2

Related Questions