Thomas
Thomas

Reputation: 34198

Find Div and set CSS using jquery

my html is stored in a variable and i just find div from that variable and set css but things are not working. here is my code

sHtml = $(sHtml).find('#frm').html();
sHtml = $(sHtml).find('#acloginpod').css({ 'width': xWidth + 'px' });  
$sHtml = $(sHtml);

what is wrong in my 3rd line. i was try to find div acloginpod from html which is stored in sHtml variable and width is stored in xWidth variable. i thinks there is problem in my code but not being able to find it out. so please help me to rectify the code. thanks

Upvotes: 0

Views: 935

Answers (2)

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

I have included comments in your code:

sHtml = $(sHtml).find('#frm').html(); // Returns a string: the .innerHTML of the element with ID frm
sHtml = $(sHtml).find('#acloginpod').css({ 'width': xWidth + 'px' }); // Returns a jQuery object of the element with ID #acloginpod
$sHtml = $(sHtml); // No need to do that sHtml is already a jQuery object

Upvotes: 1

raina77ow
raina77ow

Reputation: 106443

The problem is that you're mixing HTML strings and DOM Objects (wrapped into jQuery objects) in your code.

With this line...

$(sHtml).find('#acloginpod').css({ 'width': xWidth + 'px' });

... you convert a string with HTML (sHtml) into a DOM object, then find some element within its hierarchy, then change its property.

But in this line...

$sHtml = $(sHtml);

... you're just throw all the changes away, as you again convert into a DOM object the original string.

I wonder why don't you just...

  $sHtml = $(sHtml);
  $sHtml.find('#acloginpod').css({ width: xWidth + 'px' });

... working (and transferring) with jQuery object all the time.

Upvotes: 2

Related Questions