Edgar Navasardyan Edgar Navasardyan - 1 year ago 32
HTML Question

HTML inserts space between the first occurrence of quotes

This drives me crazy. I want to pass a string literal as a parameter to a function in HTML onclick property containing a double quote.

My HTML element looks like this:

<button onclick = "ok_button_click(""Harry Potter "")" type="button" class="btn btn-default">ok</button>

But when I load the page and open it by Inspect Element, I see a space inserted between the first quote resulting in this:

<button onclick = "ok_button_click(" "Harry Potter"")" type="button" class="btn btn-default">bad</button>

Why does the browser insert a space ???


If you are trying to pass a string value with quotes then you have to use &quot; like this:

<button onclick = "ok_button_click('&quot;Harry Potter&quot;')" type="button" class="btn btn-default">bad</button>

If you just want to pass in a string literal you can just use a single quote (or the opposite of what the attribute started with) like this:

<button onclick = "ok_button_click('Harry Potter')" type="button" class="btn btn-default">bad</button>