0

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

9
  • 2
    What do you expect from your code? Commented Oct 1, 2012 at 11:23
  • Also can you post your html code ? Commented Oct 1, 2012 at 11:23
  • sHtml is a jquery selector , or some html value? Commented Oct 1, 2012 at 11:24
  • Do you know how to debug your javascript code? Finding out what sHtml is after the first line would be helpful. Commented Oct 1, 2012 at 11:25
  • xWidth is also undefined too, please explain exactly what you're after Commented Oct 1, 2012 at 11:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Well, sHtml is indeed a jQuery object, but it's not the same as $(sHtml) - it's actually $('#acloginpod') in this series of statements.
@raina77ow an how does $(sHtml) change what was already stored in sHtml?
$(sHtml) doesn't change its value, of course. sHtml = $(sHtml) does, however.
@raina77ow If these two statements are sequential in the code, then the second one does nothing is what I meant :) sHtml = $(sHtml).find('#acloginpod').css({ 'width': xWidth + 'px' }); $sHtml = $(sHtml);
What I meant is that second statement will not capture the whole element created from sHtml string, but only its part - the element with ID 'acloginpod'. And only this element will be stored in sHtml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.