wjhplano
wjhplano

Reputation: 591

javascript document.write variables

I'm writing a simple javascript with several document.write statements...

String accountLink = "#";
String accountLogo = "img/myLogos/someLogo.png";

function drawLogos(){
    document.write('<li><a href="');
    document.write("#");  // link to account page
    document.write('"><img src="');
    document.write('img/myLogos/someLogoF.png'); // reference to logo image
    document.write('" alt=""  /></a></li>');
}

Which works just fine. but when I use a variable like this...

document.write(accountLink);

it doesn't load anything. I've tried getting rid of the quotes but nothing different happens. Is there some small syntax error I'm not picking up on?

Upvotes: 1

Views: 4991

Answers (4)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28538

You should initialize variable instead of String using var and then try:

var mytext = "Hello again";
document.write(mytext);

Here is working demo

Upvotes: 1

Ahm3d Said
Ahm3d Said

Reputation: 812

use var instead of String, Javascript is a weak typing language

<script>
        var accountLink = "#";
        var accountLogo = "img/myLogos/someLogo.png";

        function drawLogos(){
            document.write('<li><a href="');
            document.write(accountLink);  // link to account page
            document.write('"><img src="');
            document.write(accountLogo); // reference to logo image
            document.write('" alt=""  /></a></li>');
        }
</script>

Upvotes: 1

JayD
JayD

Reputation: 6521

Declare your string variables with var

  var accountLink = "#";
document.write(accountLink);

Upvotes: 1

user3152069
user3152069

Reputation: 416

Use

var accountLink = "#";
var accountLogo = "img/myLogos/someLogo.png";

Upvotes: 2

Related Questions