JavaScript How to Properly Add Double Quotes to a Textarea

I need a textarea to include a set of double quotes at the start and end of the textarea value. The below code works in that double quotes are added to the start and end of the field, but if the user enters text and then goes back to the field, multiple double quote sets can be added. How can I prevent this? A jQuery solution is also acceptable.

<textarea name="quoteName" id="quoteName" style="width:100%" rows="4" onChange="quotes();" autofocus></textarea>

function quotes(){
  var quoteValueBefore = document.getElementById("quoteName").value;
  var ensureQuotes = "\"" + quoteValueBefore + "\"";
  document.getElementById("quoteName").value = ensureQuotes;
}
Answer:1

Check to see if the text already begins with a quote, and if it already ends with one. If either is missing, add it.

Also check that the length >= 2, otherwise " would pass the test (ends with a quote? check. begins with a quote? check.)

function quotes() {
  var quoteValue = document.getElementById("quoteName").value;

  if (!quoteValue.match(/^"/))
    quoteValue = '"' + quoteValue;

  if (!quoteValue.match(/"$/))
    quoteValue += '"';

  if (quoteValue.length < 2)
    quoteValue += '"';

  document.getElementById("quoteName").value = quoteValue;
}
<textarea name="quoteName" id="quoteName" style="width:100%" rows="4" onChange="quotes();" autofocus></textarea>
Answer:2

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

  1. remove last forward slash javascript
  2. remove last forward slash c#
  3. php remove last forward slash
  4. bash remove last forward slash from string
  5. regex replace last forward slash

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

  1. json data response example
  2. json data response
  3. json data response ajax
  4. json data response jquery
  5. json response data format
  6. json response data type
  7. json response data count
  8. json response data undefined
  9. json response data python
  10. json response data model
  11. get json data response
  12. sample json data response
  13. send json data response
  14. json binary data response
  15. jbuilder json_data_response
  16. c# json data response
  17. access json response data
  18. axios json response data
  19. json.parse(response.data)
  20. laravel json response data

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...

I'm trying to write a mocha test which passes on a stream error but fails if the stream ends without an error. Detecting the error is no problem, but the finish handler is always called, even if the ...

I'm trying to write a mocha test which passes on a stream error but fails if the stream ends without an error. Detecting the error is no problem, but the finish handler is always called, even if the ...