-2

i have a button which expands and hides text. However im not sure how to change the value of the text. What i mean by that is when i click on the expand button that says "expand" it doesnt change to "hide" it stays as "expand". I tried it in javascript but wasn't completely sure how to apply it to the html :(

function btnChanger() {
    var i = 0;

    if(i % 2 == 0)
    {
        document.getElementById("hide").value="Hide";
    }
    else
    {
        document.getElementById("hide").value="Expand";
    }
    i++;
}

If it's easier to do in Jquery? Im not too sure, thank in advance :) Here is the Jfiddle: https://jsfiddle.net/dmgnsx17/3/

BriannaXD
  • 169
  • 2
  • 14

1 Answers1

1

You can do using both jQuery or JavaScript. Use this code instead of declaring any new or global variables, which might tend to attract erroneous values:

function btnChanger() {
    if (document.getElementById("hide").innerHTML == "Hide")
        document.getElementById("hide").innerHTML = "Expand";
    else
        document.getElementById("hide").innerHTML = "Hide";
}

To do the same thing in jQuery, you can use:

function btnChanger() {
    if ($("#hide").html() == "Hide")
        $("#hide").html("Expand");
    else
        $("#hide").html("Hide");
}