tags:

views:

40

answers:

6

I need this code to, if #vid is present, add 855px to its current width. If not, it should do nothing. I'm not sure how to get jQuery to add to an already existing number, but I'm sure it's pretty simple. Here is the code I have so far:

if ($("#vid").length) {
            $("#img-container").width(+=855),
        } else {
            return false;
        }
});
+1  A: 
if ($("#vid").length) 
{
    var currentWidth = $("#img-container").width();
    $("#img-container").css('width', currentWidth +855);
}
else 
{
    return false;
}
rahul
+2  A: 

You could try this:

if ($("#vid").length)
{
    $("#img-container").width($("#img-container").width() + 855);
}
chigley
This was the best answer. Thank you very much.
steve
@steve - in which case, don't forget to make it your accepted answer :) (Green check mark to the left)
chigley
Haha I'm working on it... I had to wait 7 more minutes until I could check it. :)
steve
@steve - Thanks very much :)
chigley
+1  A: 

If #vid is present? Do you mean if it is showing? Anyhow, you must put +=855 in quation marks like this "+=855px" and try css like this:

if ($("#vid").css("display") != "none") {
            $("#img-container").css("width", "+=855px");
            return false;
        } else {
            return false;
        }
});

ALSO you must add "px".

Cipi
A: 
if ($("#vid").length) {
            var vid = $("#img-container");
            vid.width(vid.width() + 855);
        } else {
            return false;
        }
});
geoff
+3  A: 

jQuerys methods do not support a += syntax (only exception: css strings), you would need to write:

$("#img-container").width($("#img-container").width() + 855)

or

$("#img-container").css("width", "+=855");
jAndy
This one was also correct, but the other one was said first. Thanks :)
steve
A: 

Just another option:

If you are wanting to animate it

$("#img-container").animate({width: "+=855px"});
tster