insanity
insanity

Reputation: 1178

adjust the source of an image

I have a div. I want to add an image to a div using jquery.

What i am doing

$('#b').prepend('<img id="theImg" src="numbers/"+rand+".png" />');

but it is not working. I want the src attribute to be as numbers/rand.png where rand is a variable. How can i make it work ?

I have tried this in a different way too

Here is what i have tried

        var img1 = document.createElement('img');

        // give it an id
        img1.attr("id");
        alert(img1.show());
        // source, link
        img1.attr('src','"numbers/"+rand+".png"');

        $("#b").appendTo(img1);

This also not worked. Where am i doing wrong ? Please help

Upvotes: 1

Views: 18

Answers (1)

j08691
j08691

Reputation: 207901

Try:

$('#b').prepend('<img id="theImg" src="numbers/' + rand + '.png" />');

You were using the wrong type of quotes around "+rand+".

Upvotes: 2

Related Questions