JavaScript Javascript function that toggles text of different buttons javascript function that returns a function,javascript function that returns

I'm very new to javascript and web development. I'm building a page that has multiple areas for users to add and save text in. Each text area has a Button with different ID that I use to toggle the text areas between readonly and editable and the text of the buttons "Save" when the text area is editable and "Edit" when the text is read only.

The function below works for 1 button (the editNameBtn). My question is is there is a way to write one function that I can pass the ID of each button to to toggle the text? Rather than having a different function for each button w' different id.

function toggleText(editNameBtn) {

 var el = document.getElementById("editNameBtn").textContent;

  if (el == "Edit"){
    el = "Save";
  } else {
    el = "Edit";
  }
  document.getElementById("editNameBtn").textContent = el;
}
Answer:1
 function toggleText(editNameBtn) {
 var el = document.getElementById("editNameBtn").textContent;
  if (el == "Edit"){
    el = "Save";
  } else {
    el = "Edit";
  }
  document.getElementById("editNameBtn").textContent = el;
}

In the above functions in getElementById you are passing editNameBtn as string but it is an argumrnt. Please remove the quotes. The below snipet address the isuee

 function toggleText(editNameBtn) {
 var el = document.getElementById(editNameBtn).textContent;
  if (el == "Edit"){
    el = "Save";
  } else {
    el = "Edit";
  }
  document.getElementById(editNameBtn).textContent = el;
  
}
<button id="PASSIDHERE" onclick="toggleText('PASSIDHERE')">Edit</button>
<!-- single qutoes in toggleText('PASSIDHERE') and the argument should be same as id -->
Answer:2

You were close. You need to pass the parameter of the function as is rather than wrapping it with double quotes.

change

var el = document.getElementById("editNameBtn").textContent;

to

var el = document.getElementById(editNameBtn).textContent;
Answer:3

Just pass in the ID of the button to your function, as a string.

function toggleText(buttonID) {
    var el = document.getElementById(buttonID).textContent;

    if (el == "Edit") {
        el = "Save";
    } else {
        el = "Edit";
    }

    document.getElementById(buttonID).textContent = el;
}
Answer:4

Your code should be updated like this.

function toggleText(btnId) {

 var el = document.getElementById(btnId).innerHTML;


  if (el == "Edit"){
    el = "Save";
  } else {
    el = "Edit";
  }
  document.getElementById(btnId).innerHTML= el;
}

And

Your Html should be like this

<button id='editNameBtn' onclick='toggleText("editNameBtn")'>Edit</button>
Answer:5

I need to clear the selected value of select dropdown as soon as user focus on the select element. I have used the below code but cant figure out what property of select do I need to reset. <...

I need to clear the selected value of select dropdown as soon as user focus on the select element. I have used the below code but cant figure out what property of select do I need to reset. <...

In input, I have a number of seconds. In output, I want minutes and seconds, on two digits, separated by : e.g. 1 -> 00:01 72 -> 01:12 I come up with the following code : formatTime: function(...

In input, I have a number of seconds. In output, I want minutes and seconds, on two digits, separated by : e.g. 1 -> 00:01 72 -> 01:12 I come up with the following code : formatTime: function(...

  1. format time javascript moment
  2. format time javascript
  3. format time javascript date
  4. time format javascript am pm
  5. time format javascript example
  6. time format javascript locale
  7. javascript timestamp format
  8. format time string javascript
  9. format utc time javascript
  10. format epoch time javascript
  11. format current time javascript
  12. get format time javascript
  13. change format time javascript
  14. format time duration javascript
  15. input format time javascript
  16. format time output javascript
  17. iso time format javascript
  18. convert time format javascript
  19. format time 24 hour javascript
  20. tz time format javascript

I have data from the server to display. I receive strings such as this: 2016-05-01. I need to filter the data between two dates in that format. I have a function to parse the date as it is: $scope....

I have data from the server to display. I receive strings such as this: 2016-05-01. I need to filter the data between two dates in that format. I have a function to parse the date as it is: $scope....

  1. parse string into array
  2. parse string into int
  3. parse string into json
  4. parse string into list python
  5. parse string into int java
  6. parse string into json java
  7. parse string into array python
  8. parse string into datetime python
  9. parse string into object javascript
  10. parse string into date
  11. parse string into double java
  12. parse string into list
  13. parse string into int python
  14. parse string into array java
  15. parse string into datetime c#
  16. parse string into words c++
  17. parse string into object java
  18. parse string into int c#
  19. parse string into vector c++
  20. parse string into json python

I'm trying to add the value of the checkbox in the field the is closest to it. But I think I missed something. Please see my code below: $(document).ready(function(){ $("input:checkbox")....

I'm trying to add the value of the checkbox in the field the is closest to it. But I think I missed something. Please see my code below: $(document).ready(function(){ $("input:checkbox")....

  1. pass input checkbox value to controller