rex
rex

Reputation: 1025

img src variable not working in jQuery

The application is working on dreamweaver but when I try it on Chrome or IE it dones't work. I know where the issue is but I don't now how to fix it. It has something to do with applying variable as an img src. The code is setup at http://jsfiddle.net/v9rxH/

the issue is in the line

 $(".topImage").attr("src", "'" + obj[randomNumA].urlTop + "'");
 $(".middleImage").attr("src", "'" + obj[randomNumB].urlMiddle + "'");
 $(".bottomImage").attr("src", "'" + obj[randomNumC].urlBottom + "'");

when the browser renders it is show

 <img src="'http://placehold.it/300x100&text=SecondBottom'" class="bottomImage">

it's adding additional single quotes(' ') in src. Based on that, I removed the single quotes and it shows the variable as text instead of it's value. What am I missing? Thanks in advance. Rex

Upvotes: 1

Views: 432

Answers (4)

j08691
j08691

Reputation: 207901

Use this instead:

$(".topImage").attr("src", obj[randomNumA].urlTop);
$(".middleImage").attr("src", obj[randomNumB].urlMiddle);
$(".bottomImage").attr("src", obj[randomNumC].urlBottom);

jsFiddle example.

Upvotes: 1

calebds
calebds

Reputation: 26228

Remove single quotes from obj[randomNumN].urlTop.

Upvotes: 2

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Try,

 $(".topImage").attr("src", obj[randomNumA].urlTop);
 $(".middleImage").attr("src", obj[randomNumB].urlMiddle);
 $(".bottomImage").attr("src", obj[randomNumC].urlBottom);

Upvotes: 1

Joe
Joe

Reputation: 15802

You're putting the single quotes around it with "'" - you don't need to, using .attr() handles that for you:

$(".topImage").attr("src", obj[randomNumA].urlTop);
$(".middleImage").attr("src", obj[randomNumB].urlMiddle);
$(".bottomImage").attr("src", obj[randomNumC].urlBottom);

Upvotes: 6

Related Questions