tags:

views:

66

answers:

3

Hi,

I have this bit jQuery with HTML. Unfortunately its not working correctly. Can anyone please advice how it should be please?

var Bred =("<div class='TakeMeTo' style='width:" + width + "px; right: " + width + "'>" + whereTo + "</div>");

Thank you for your help in advance

+1  A: 

I'm not sure what you mean by "it's not working correctly". What exactly is happening? Whatever the case, you're usually better off constructing dynamic HTML this way:

var Bred = $("<div></div>").addClass("TakeMeTo")
  .css("width", width + "px").css("right", width)
  .text(whereTo);

Use css() instead of directly setting the style attribute. Optionally you may wish to use .html(whereTo) instead, whichever is appropriate.

Why do it this way? Things will be correctly escaped.

cletus
This is my result from string I written above<div class="TakeMeTo" style="width: 91px;">Some function</div>missing right in css
Dom
+1  A: 

I think I found he root of your problem

you need to specify a px unit for the "right" css property, like below:

 var Bred =("<div class='TakeMeTo' style='width:" + width + "px; right: " + width + "px;'>" + whereTo + "</div>");

Notice the second [ "px;'>" ] after [ right: " + width + ]

pǝlɐɥʞ
A: 

This one works nice. Thanks you all for suggestions

var Bred = $("<div></div>").attr("class", "TakeMeTo").attr("style", "width:" + width + "px; right: " +  width  + "px").text(whereTo);
Dom
Please mark ekhaled's solution as correct.
Alex Bagnolini