0

I'm trying to make a super simple "see more.." link. I was doing it before with a button, but it didn't work very well on mobile, so I've changed it to a link, but I'm getting this error: jquery-3.1.1.min.js:2 jQuery.Deferred exception: seeMore is not a function TypeError: seeMore is not a function

Here's the code. I reckon it's just a stupid syntax error somewhere.

$(document).ready(function(){
    var seeMore = document.getElementById('seeMore');

    seeMore.onclick=seeMore();

    function seeMore(){
        document.getElementById('infoText').innerHTML= 'More text';
    };

    function seeLess(){
        document.getElementById('infoText').innerHTML= 'Less text';
    };
});

and here's the linked seeMore ID.

<a href="#" id="seeMore">See More</a>

What's going on here??

4
  • 7
    Name conflict. You used the same name for a variable and function. Commented Jul 4, 2017 at 10:54
  • As the error says. See more is not a function. Commented Jul 4, 2017 at 10:54
  • 1
    Plus you need to assign the function itself after renaming seeMoreEl.onclick=seeMore not seeMoreEl.onclick=seeMore() Commented Jul 4, 2017 at 10:55
  • Also, to assign a function as a handler, do not use braces beause doing so will execute the method instead of treating it as an object. Here is how to do it: seeMore.onclick=myFunction; Commented Jul 4, 2017 at 10:56

1 Answer 1

1

You use the same name for a property and a function and therefore its not clear what you mean.

You could rename one variable.

Also when you assign seeMore() with the brackets you assign the return value of the function and not the function itself. You need to remove the brackets.

You could also use the events which jquery provide to easily support all browsers.

$(document).ready(function(){
    var seeMoreElement = document.getElementById('seeMore');

    function seeMore(){
        document.getElementById('infoText').innerHTML= 'More text';
    };

    function seeLess(){
        document.getElementById('infoText').innerHTML= 'Less text';
    };

    $(seeMoreElement).click(seeMore);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man! I've been looking at this code for far too long. Although, it's still not working very well on mobile. I'll work it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.