0

i pass this to a div innerhtml from javascript.

when i do it like this it doesn't work. it should display an image hyperlink

var newtext4=("<a href=" + number + "><img src=" + newtext2 + " /></a>");

if i do just text, it displays a hyperlink.

var newtext4=("<a href=" + number + ">" + newtext2 + "</a>");

any idea why?

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by fxm
0
<script language="javaScript">
var number="10.html";
var newtext2="test.jpg";
var newtext4=("<a href=" + number + "><img src=" + newtext2 + " /></a>");
alert (newtext4);
document.write(newtext4);
</script>

The above is working for me.

0

var newtext4=("<a href=" + number + "><img src=" + newtext2 + " /></a>");

If number or newtext2 contains spaces or certain characters that code won't work - and it may even break the entire page.

At a minimum, you need another layer of quotes

"<a href='" + number + "'><img src='" + newtext2 + "' /></a>"

and even then you may still need to validate the content of those two variables.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.