Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2011, 10:58 PM   PM User | #1
ole90
Regular Coder

 
Join Date: Jan 2007
Posts: 217
Thanks: 9
Thanked 0 Times in 0 Posts
ole90 is an unknown quantity at this point
jquery adding CSS to newly added div via append()

Hey guys,

I have a situation where I append a div to my page via an ajax call. I then want to update the CSS for it and access the property. For example
Code:
$('#container').append(\"<div id='char_'+d[0]><img src=\" + st + \"></div>\");
$('#char_'+d[0]).css({'position' :  'absolute', 'left' : '425', 'top' : '860'});
The problem is, no css is applied to my newly added div. The d[0] is an unique id and is used to give the div a unique name.

Any help from the gurus?
ole90 is offline   Reply With Quote
Old 02-28-2011, 01:30 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,609
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
First I don’t see where you have an AJAX call here. Can we see more than those two lines of code? Secondly you should avoid mixing JS and CSS (just as you should avoid mixing HTML and CSS or JS and HTML). Rather, you could assign a class to that appended element and style it in the stylesheet using that class then.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 02-28-2011, 11:01 AM   PM User | #3
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
Originally Posted by ole90 View Post
Code:
$('#char_'+d[0]).css({'position' :  'absolute', 'left' : '425', 'top' : '860'});
This is bogus. The values for the 'left' and 'top' properties have to be either numbers or strings containing real CSS syntax. So, that's either 425 or '425px'.
venegal is offline   Reply With Quote
Old 03-01-2011, 05:56 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,462
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
might be too soon, or there's an ID conflict.

fix your quotes below, but splitting it up into creation/appending phases gives you the extra handle you need to call .css() on an object.

Code:
var temp=$("<div id='char_'+d[0]><img src=\" + st + \"></div>");
$('#container').append(temp);
temp.css({'position' :  'absolute', 'left' : '425', 'top' : '860'});

PS: numerical <WIDTH> attribs without specified units default to pixels coords.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is online now   Reply With Quote
Old 03-01-2011, 02:43 PM   PM User | #5
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
Originally Posted by rnd me View Post
fix your quotes below, but splitting it up into creation/appending phases gives you the extra handle you need to call .css() on an object.

Code:
var temp=$("<div id='char_'+d[0]><img src=\" + st + \"></div>");
$('#container').append(temp);
temp.css({'position' :  'absolute', 'left' : '425', 'top' : '860'});
Apart from the fact that this saves a DOM query, this is not really an issue, and the other approach should work just fine.

There are a bunch of other issues, though:
Code:
$('#container').append(\"<div id='char_'+d[0]><img src=\" + st + \"></div>\");
rnd me already addressed that in his snippet, but it's worth pointing out: You can't escape your quotes like that. Why would you even do that?

Code:
$('#container').append(\"<div id='char_'+d[0]><img src=\" + st + \"></div>\");
There is no variable substitution in Javascript, so those are the literal strings "+d[0]" and " + st + ", which is not what you want.

A working version of the code could look like that:

PHP Code:
$('#container').append('<div id="char_' d[0] + '"><img src="' st '"></div>');
$(
'#char_' d[0]).css({'position' :  'absolute''left' 425'top' 860}); 
But I'd definitely prefer real DOM manipulation over innerHTML:


PHP Code:
$('<div/>', {
    
id'char_' d[0],
    
css: {
        
position'absolute',
        
left425,
        
top860
    
}
}).
append(
    $(
'<img/>', {
        
srcst,
        
alt'whatever'
    
})
).
appendTo('#container'); 
venegal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 PM.


Advertisement
Log in to turn off these ads.