/    Sign up×
Community /Pin to ProfileBookmark

Chrome setAttribute (class)

I have the following code (within an iframe), and it works. In Chrome, the class that I am using never is applied to the image. It gets set as an attribute in the tag, but the style is not applied. How can I fix this, or is this a Chrome bug?

[code]<script type=”text/javascript”>
var is_cover = <?php echo $is_cover; ?>;
var ni = parent.document.getElementById(‘albumPhotos’);
try{
var itm = ‘<img class=”photoAlbum”‘;
if(is_cover==’1’){
itm += ‘ style=”background-color:#cccccc;”‘;
}
itm += ‘ onclick=”loadBody(‘photos’,’id’,'<?php echo mysql_insert_id(); ?>’);” src=”http://img.publicsize.com/photo/<?php echo $dir; ?>/thumb/<?php echo $file; ?>.jpg” />’;
var newdiv = parent.document.createElement(itm);
}catch(e){
var newdiv = document.createElement(‘img’);
newdiv.setAttribute(‘onclick’, “loadBody(‘photos’,’id’,'<?php echo mysql_insert_id(); ?>’);”);
newdiv.setAttribute(‘class’, “photoAlbum”);
newdiv.setAttribute(‘src’, “http://img.publicsize.com/photo/<?php echo $dir; ?>/thumb/<?php echo $file; ?>.jpg”);
if(is_cover==’1′){
newdiv.setAttribute(‘style’, ‘background-color:#cccccc;’);
}
}

if(is_cover==’0′){
ni.insertBefore(newdiv, ni.firstChild.nextSibling);
}else if(is_cover==’1′){
ni.insertBefore(newdiv, ni.firstChild);
}
</script>[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineOct 25.2010 — Never use setAttribute unless direct assignment doesn't work, which in my experience only occurs with attributes passed to plugins.

Try: [CODE]newdiv.className = "photoAlbum";[/CODE]
Copy linkTweet thisAlerts:
@FangOct 25.2010 — and onclick[CODE]newdiv.onclick = function() {loadBody('photos','id','<?php echo mysql_insert_id(); ?>');};
[/CODE]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 25.2010 — @Sterling Isfine, That doesn't work ? it inserts it into the tag but that is it.

@Fang, yours didn't even add it to the tag.
Copy linkTweet thisAlerts:
@FangOct 26.2010 — If it doesn't work then there is another problem with your script.

Show the generated code.

Another problem:[CODE]newdiv.style.backgroundColor = '#ccc;';
[/CODE]
Copy linkTweet thisAlerts:
@KorOct 26.2010 — Why do you use that try/catch sequence? simply use standard DOM methods (with the DOM0 crossbrowser workarounds regarding the [B]element.className='classname';[/B], [B]element.style.backgroundColor='#ccc'[/B] and [B]element.onclick=function(){someFunction()}[/B])
Copy linkTweet thisAlerts:
@Sterling_IsfineOct 26.2010 — @Sterling Isfine, That doesn't work ? it inserts it into the tag but that is it.
[/QUOTE]
I don't know what that means but the line will apply the CSS class, so the issue must be elsewhere.
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...