Arunprasanth K V
Arunprasanth K V

Reputation: 21931

How to write single quotes inside a double quotes in string inputted Html Elements

string mystring == "<input class=\"success\" type=\"button\" id=\"Delegate_New_btn\" value=\"New\" onclick=\"location.href="+mylocation+"'/>";

I have a string like above.

The problem is when I innerHTML it to a div like below, then it shows incorrect syntax error :

document.getElementById("id").innerHTML=mystring;

What I need is :

<input type="button" value"somevalue" onclick="location.href='mylocationurl'"/>

I don't know how to include this single quotes inside a double quotes. I had tried back slash method but I also failed to implement that.

Note : i had define this in my serverside C# and pass it to client side Please help me to solve this.

More information

VM.Custombuttons_tag = "<td><input class=\"btn btn-mini btn-success\" type=\"button\" id=\"Delegate_New_btn\" value=\"New\" onclick='location.href=\'" + string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~")) + "/ABC/Delegate/AssignListUserLevel?PeriodId=0'/></td>";

this is my full code here VM is a method and Custombuttons_tag is string so when ever i run my code it will innerHTML the code to a div and it shows some incorrect syntax error

Upvotes: 0

Views: 574

Answers (4)

Waxi
Waxi

Reputation: 1651

You can mix-match single and double quotes as long as they are nested properly.

var mystring = '<input class="success" type="button" id="Delegate_New_btn" value="New" onclick="location.href=' + mylocation + '"/>';

Upvotes: 0

pattyd
pattyd

Reputation: 6160

You have a few problems here, the first being that you do not use == when assigning variables, and the second being that you need to use var rather than string.

So, your code should look like this: var mystring = "<input class='success' type='button' id='Delegate_New_btn' value='New' onclick='location.href=\'+mylocation+\''/>";

The only time that you need to use \' is if your single quotes are included in single quotes, like you have with the location.href part.

Upvotes: 0

Adam MacDonald
Adam MacDonald

Reputation: 1958

You may want to try something like this ...

var input=document.createElement('input');
input.setAttribute('class','success');
input.setAttribute('type','button');
input.setAttribute('id','Delegate_New_btn');
input.setAttribute('value','New');
input.setAttribute('onclick','location.href="'+mylocation+'"');

document.getElementById('id').appendChild(input);

Upvotes: 0

KJ Price
KJ Price

Reputation: 5964

You do not define a string using string, you define everything using var:

var myString = "I am a string";
var myNumber = 1000;

Java and JavaScript do have their differences!

Upvotes: 2

Related Questions