-2

this is my original html codes:

<div>
    <img src="image/sample.jpg" onmouseover="showTitle('title')">
</div>

But I need it to be converted to javascript, for condition if {{IMAGE(z1)}} do exist, then var x, would be written out as html.

 <script>
    if ("{{IMAGE(z1)}}" != "") { var x = "<img src="image/sample.jpg" onmouseover='showTitle('title')'></div>"; 
    document.write(x); } 
 </script>

The image does show; however, it wont read/execute the related javascript functions.

HTML:

onmouseover="showTitle('title')"

Javascript variable double quote (failed):

var x = "onmouseover="showTitle('title')""

Javascript variable single quote (failed):

var x = "onmouseover='showTitle('title')'"

Do you know a workaround of this? Thanks in advance!!

Dylan Daicy Siao
  • 155
  • 1
  • 3
  • 13

3 Answers3

1
var x = "<img src=\"image/sample.jpg\" onmouseover=\"showTitle('title')\"></div>"; 
Mike Loffland
  • 344
  • 1
  • 10
1

Note you are missing the starting <div> and you need to ensure to escape quotes. Try and use single quotes inside double quotes and vise-versa, only escape when you have to:

var x = "<div><img src='image/sample.jpg' onmouseover='showTitle(\"title\")'></div>"; 

When you want to place a double quote inside double quotes you need to escape the inside with \" the same applies to single quotes \':

" \" " -- Double Quotes Escape
' \' ' -- Single Quotes Escape

Also note unless you are using a library that interprets {{ .. }}, this condition "{{IMAGE(z1)}}" != "" will always be true.

Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

Main error : change ,

 var x = "<img src="image/sample.jpg" onmouseover='showTitle('title')'></div>"; 

To

 var x = "<img src=\"image/sample.jpg\" onmouseover=\"showTitle('title')\"></div>"; 

By the way your method is actually bad.

I wonder if that if would work... I dont know much of js but i would say, leave that method.

Second, use jquery if possible .

U can simple do

$("#i").mouseover(function(){
showTitle('title'); });

Where i is the id of image

frunkad
  • 2,433
  • 1
  • 23
  • 35