简体   繁体   中英

How to put quotes around a getElementById quote + string?

I have been working on this assignment where I have to make a prompt in which the visitor puts types in a string (or just any sentence for this matter) and afterwards the typed text needs to be shown on the page itself alongside with an indication in what line the first spacebar is implemented.

The problem however is that i need to put this in quote signs and since I am using a "+ script" in my Code, I cannot put it inside quotes.

Here is the code I am using:

 <body> <p id="Result"> </p> <p id="First Spacebar"></p> <script> let string = prompt("Put text here"); const text = string; document.getElementById('Result').innerHTML = "You said: " + string document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" ")); </script> </body> 

Everything is working like it should, but i can't seem to get the " signs on each end of the string in the webpage version.

You can use template strings

 <body> <p id="Result"> </p> <p id="First Spacebar"></p> <script> let string = prompt("Put text here"); const text = string; document.getElementById('Result').innerHTML = `You said: "${string}"` document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" ")); </script> </body> 

You can either escape the " that are meant to be displayed ( "..\\"" ), or , you can enclose your strings in single quotes ' and freely use double quotes " inside it:

 let string = prompt("Put text here"); const text = string; document.getElementById('Result').innerHTML = 'You said: "' + string + '"'; document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" ")); 
 <p id="Result"></p> <p id="First Spacebar"></p> 

With ES6 template literals you can do it like this:

`You said: "${string}"`

Old style, you can leverage the fact that you can use either " or ' as outer quotation marks:

'You said: "' + string + '"'

Here are 2 ways:

  1. document.getElementById('Result').innerHTML = 'You said: "' + string + '"'

(this uses both kinds of quotation marks so one can be used inside strings)

  1. document.getElementById('Result').innerHTML = "You said: \\"" + string + "\\""

(this uses the escape character, '`', to indicate quotation marks that should be used as normal characters instead of surrounding strings)

You can use javascript escape codes. You can find an easy conversion table for these escape codes here .

The javascript escape code for " is " .

A full script example:

 <body> <p id="Result"> </p> <p id="First Spacebar"></p> <script> let string = prompt("Put text here"); const text = string; document.getElementById('Result').innerHTML = "You said: "" + string + """; document.getElementById('First Spacebar').innerHTML = "The first spacebar bevindt can be found in line: " + string.indexOf((" ")); </script> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM