tags:

views:

53

answers:

3

--In the variable line below I declared a variable (xwid) as 690. But when I try the code by using the variable of $xwid, the width never updates. Any ideas?

$xwid=690;  // my var

// my code
$('.iframe-link').html( '<iframe src="reasons.html" frameborder="0" width=$xwid height="305" scrolling="auto">
A: 

Variables in Javascript are not declared this way

$xwid=690;     // wrong
alert($xwid);  // wrong

var xwid = 690;  // right
xwid = 690;      // right
alert(xwid);     // right

EDIT: Your code should look like thiis

var xwid=690;  // my var

// my code
$('.iframe-link').html('<iframe src="reasons.html" frameborder="0" width="'+xwid+'" height="305" scrolling="auto">');
Tim
Tim - I guess this is your solution I used, my mistake. Thanks so much.
sonny5
A: 

Unless you mean to declare your variables as global, you need to put a 'var' in front. Hence you would declare the variable in your code as:

var $wxid = 690;

Also, you don't need to put the dollar symbol in front of the variable name. It isn't bad (in that it will work fine) but it isn't required and looks a bit strange.

Now to answer the question. The problem you are having is that unlike PHP, Javascript doesn't expand variables in strings.You need to do concatenate the strings using the + operator. Something like this

$('.iframe-link').html('<iframe src="reasons.html" frameborder="0" width="'+$xwid+'" height="305" scrolling="auto">');
Yacoby
Yacoby - it works perfectly!!! var xwid = 690; .......... width="'+xwid+'" ...... did the trickThanks to you and the team. Sonny5
sonny5
A: 

I would say

var xwid=690;  // my var

// my code
$(".iframe-link").html( '<iframe src="reasons.html" frameborder="0" width='+xwid+' height="305" scrolling="auto">');
Javi