0

I have a few buttons each with a specific class to differentiate it from other similar buttons. Class name follows the format moreid where id changes with button. I am using following code to change button text:

function changeText(btn){
  $('.'+btn).text(function(i, text){
    return text === "Show More" ? "Show Less" : "Show More";}
)}

I call this function using changeText(moreid). With this code I get the error:

Error: Syntax error, unrecognized expression: .[object HTMLDivElement]

This is the HTML of button

<button type="button" 
        class="btn btn-primary btn-lg moreapple" 
        data-toggle="collapse" 
        data-target="#moreapple" 
        onclick="changeText(moreapple)">Show More</button>

The only thing that change from one button to another is moreapple to morenews etc.

How can I change this function to change button text?

Sujata Hulsurkar
  • 115
  • 2
  • 12

3 Answers3

1

Your changeText(btn) tells that, you're passing btn as an argument, so you might have to give this a try

$(btn).text(function(i, text){
    return text === "Show More" ? "Show Less" : "Show More";
});

Your markup should be like this unless you've specific requirements

<button type="button" 
        class="btn btn-primary btn-lg moreapple" 
        data-toggle="collapse" 
        data-target="#moreapple" 
        onclick="changeText(this)">Show More</button>

use $(btn) as it comes from the calling function changeText()

Fiddle Example

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
0

Obviously your Id's would be different depending on your naming convention Duplicate Question Answer

var status = "less";

function toggleText()
{
    var text="Here is some text that I want added to the HTML file";

    if (status == "less") {
        document.getElementById("textArea").innerHTML=text;
        document.getElementById("toggleButton").innerText = "See Less";
        status = "more";
    } else if (status == "more") {
        document.getElementById("textArea").innerHTML = "";
        document.getElementById("toggleButton").innerText = "See More";
        status = "less"
    }
}
Community
  • 1
  • 1
Huang Chen
  • 1,177
  • 9
  • 24
0

You need to update your markup from

<button type="button" 
        class="btn btn-primary btn-lg moreapple" 
        data-toggle="collapse" 
        data-target="#moreapple" 
        onclick="changeText(moreapple)">Show More</button>

to

<button type="button" 
        class="btn btn-primary btn-lg moreapple" 
        data-toggle="collapse" 
        data-target="#moreapple" 
        onclick="changeText('moreapple')">Show More</button> <!-- Pass moreapple as string -->
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59