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?

Member Avatar for rajarajan2017
<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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.