By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

setAttribute() not working well in IE :(

100+
P: 130
hello all,
first i would like to say thanks for all the answers in my last posts i dont like to post just thanks so acoder thanks man :)

now i have a problem with IE (or it have a problemm with me)
i worte this code that generate <a> tag, useing innerHTML
snipt:

Expand|Select|Wrap|Line Numbers
  1. function gen_link(){
  2.    var x = document.getElementById("link");
  3.    x.innerHTML = "<a href='http://www.google.com' id='_link'><img width='49' height='61' id='_img'></a> ";
  4. }
now the link is being generated and everything is allright
the problem start when i want to set the src of the <img>
Expand|Select|Wrap|Line Numbers
  1. var img_s = document.getElementById('_img');
  2. img_s.setAttribute("src","http://some_url_with_a_pic.com");
  3.  
the script working good on FF but in IE gives me error that the object doesnt support this action

i read in the web a bit and understand that IE dont support setAttribute() 100%
and becuze the innerHTML created the <img> tag IE cant "see"
what can i do to pass that obstacle
Sep 23 '08 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 392
Try this

Expand|Select|Wrap|Line Numbers
  1. img_s.src = 'http://some_url_with_a_pic.com';
  2.  
If that does not work then make sure that the "_img" element is actually in the page when this logic is being called. If you are trying to set that value before that element is being created you will get an error.
Sep 23 '08 #2

Dormilich
Expert Mod 5K+
P: 8,639
a substitute for setAttribute() are the older DOM methods:
Expand|Select|Wrap|Line Numbers
  1. var A = document.createAttribute({attribute_name});
  2. A.nodeValue = {attribute_value};
  3. {your_element}.appendChild(A);
but in your case I'd prefer pronerdís solution too.

regards
Sep 23 '08 #3

dmjpro
100+
P: 2,476
a substitute for setAttribute() are the older DOM methods:
Expand|Select|Wrap|Line Numbers
  1. var A = document.createAttribute({attribute_name});
  2. A.nodeValue = {attribute_value};
  3. {your_element}.appendChild(A);
but in your case I'd prefer pronerdís solution too.

regards

Why setAttribute works odd in IE?
I also have the same problem :(
Have a look at the thread ...
Thanks!
Sep 24 '08 #4

Dormilich
Expert Mod 5K+
P: 8,639
i read in the web a bit and understand that IE dont support setAttribute() 100%
do I need to say more?
Sep 24 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.