Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-line string insert using jQuery

$("#addSelect").click(function() {         $("#optionsForm").after("Hello world."); } ); 

This works.

$("#addSelect").click(function() {         $("#optionsForm").after("<tr>     <td><input type="text" class="optionField" value="Options" /></td>     <td>         <ul class="option">             <li><select><option>Value..</option></select></li>         </ul>     </td> </tr>"); } ); 

Something like this doesn't.

In Chrome, I get the error "Unexpected token ILLEGAL". After Googling I've discovered my teeny brain doesn't know much about javascript and multi-lines. So I added '\' to then end of each line. Yet, I now get the error "Unexpected identifier".

I'd like this to not be as difficult as I'm making it :)

like image 543
Luke Burns Avatar asked Jun 25 '10 03:06

Luke Burns


People also ask

How to write Multiline string in jQuery?

This works. $("#addSelect"). click(function() { $("#optionsForm"). after("<tr> <td><input type="text" class="optionField" value="Options" /></td> <td> <ul class="option"> <li><select><option>Value..

How to add a Multi line string js?

Answer: Use String Concatenation You can use string concatenation (through + operator) to create a multi-line string.

What is the delimiter for multi line strings in JavaScript?

Method 1: Multiline-strings are created by using template literals. The strings are delimited using backticks, unlike normal single/double quotes delimiter.

How do you write multi line strings in template literals in JavaScript?

Template literals are strings delimited by backticks, instead of the normal single/double quote delimiter. They have a unique feature: they allow multiline strings: const multilineString = `A string on multiple lines` const anotherMultilineString = `Hey this is cool a multiline st r i n g ! `


1 Answers

Change all the double quotes for the attributes to single quotes.

$("#addSelect").click(function() {          $("#optionsForm").after("<tr> \     <td><input type='text' class='optionField' value='Options' /></td> \     <td> \         <ul class='option'> \             <li><select><option>Value..</option></select></li> \         </ul> \     </td> \ </tr>");  } );  
like image 93
rahul Avatar answered Sep 22 '22 18:09

rahul