Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text via jQuery?

I am trying to change the "show more" text depending on the state. This isn't working:

        <div id="description">
        text goes here
            </div>
            <div id="more-less-container">
                <a href="#" id="more-less">expand / collapse</a>
            </div>  
            <script>
            var open = false;
            $('#more-less').click(function() {
                if (open) {
                    $('#description').animate({height:'10em'}); 
                    $('#more-less').innerHTML ='show less';             
                }
                else {
                    $('#description').animate({height:'100%'});
                            $('#more-less').innerHTML ='show more'; 
                }
                open = !open;
            });
            </script>
like image 418
Will Curran Avatar asked Dec 08 '25 20:12

Will Curran


2 Answers

Use $('#more-less').text('show more'); instead of innerHTML.

jQuery wraps DOM Elements into jQuery objects, if you would want to use the innerHTML property, you could use the .html() function instead - but .text() is better as it will html-escape the content.

The alternative, to really access innerHTML property, is to get the DOM Element out of the jQuery object, as such: $('#more-less')[0].innerHTML = 'show more';.

like image 75
Seldaek Avatar answered Dec 10 '25 09:12

Seldaek


I believe that you can use something like

$('#more-less').html("show more");

or

$('#more-less').html("show less");
like image 33
Rion Williams Avatar answered Dec 10 '25 09:12

Rion Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!