• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I nest Strings within Strings?

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a String:

document.write("Hello!")

Here's a String in a String:

onClick='myMethod("SomeText")'

How do I go further to have a String in a String in a String.... and beyond? I just started in JavaScript a few days ago and I am already finding myself dodging this issue so I know I will hit it later.

Thanks in advance!
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Escape the quote characters with the \.

E.g. 'My name is O\'Brien'
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. I don't think you get what I mean though. Take the following command...

onClick='addLink("<a href='www.mylink.com'>Hello!</a>")'

This is just an example, but as you can see my onClick takes a String. Inside of that there is another String which is some html code. Inside of that String there is another String for an Href link. As soon as it reads the ' before the www the onClick is going to think it's at the end of the statement when it's not. I'm wanting to know how to handle these types of situations where you have more than 2 Strings nested inside each other.

I realize that the previous example would use \"www.mylink.com\" to escape the quotes... so it's a bad example, but I hope you get my question.

Thanks!

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer remains the same.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Answer remains the same.


Actually is does not in this case. You can not escape the single quote with a \ since we are dealing with an HTML attribute and not a plain old JavaScript string.

In that case you need to use an entities



OR

you need to change it up and use the escaping Bear talked about



Eric
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. Thanks for the help guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic