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?

5
  • Show us your html code, also, how are you calling that function? Which is the param? Commented Jul 28, 2015 at 17:20
  • need the html code as well. your code works fine. try this jsfiddle.net/re0218gg Commented Jul 28, 2015 at 17:24
  • jsfiddle.net/mohamedyousef1980/emzuk1um Commented Jul 28, 2015 at 17:26
  • @leo.fcx and Sushil I have added the HTML code. Commented Jul 28, 2015 at 17:27
  • you should be passing changeText('moreapple') instead of changeText(moreapple) Commented Jul 28, 2015 at 17:28

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

is btn used because of the class name btn?
@SujataHulsurkar It's a good practice to pass this if you want to target clicked element rather than class name, because in DOM there will be many elements having the same class.
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"
    }
}

Comments

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 -->

Comments

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.