absorr Posted May 23, 2012 Share Posted May 23, 2012 How do I add an attribute (like src="blah") into a tag created using createElement? It has to be in JS, and can not use innerHTML. Thanks! Link to comment Share on other sites More sharing options...
Don E Posted May 23, 2012 Share Posted May 23, 2012 Check out this link.. might give you an idea: http://w3schools.invisionzone.com/index.php?showtopic=43494 Link to comment Share on other sites More sharing options...
ShadowMage Posted May 23, 2012 Share Posted May 23, 2012 Usually, you just do: element.attributeName = ...; where attributeName is the name of the attribute you're trying to set. However, some are a little different. For example, to set the class attribute you'd do: element.className instead of just element.class Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2012 Share Posted May 24, 2012 Usually, you just do:element.attributeName = ...; where attributeName is the name of the attribute you're trying to set. However, some are a little different. For example, to set the class attribute you'd do: element.className instead of just element.class Technically, those aren't attributes. They're properties belonging to the DOM element. For most attributes defined by the W3C browsers create a property. If you want to access a non-standard attribute you will need to use the .getAttribute() method. Link to comment Share on other sites More sharing options...
eTianbun Posted May 24, 2012 Share Posted May 24, 2012 Technically, those aren't attributes. They're properties belonging to the DOM element... Thats currect bro, but since those properties represent HTML element's attributes, i think it can also be called attributes (Just my own opinion). Link to comment Share on other sites More sharing options...
ShadowMage Posted May 25, 2012 Share Posted May 25, 2012 Technically, those aren't attributes. They're properties belonging to the DOM element. For most attributes defined by the W3C browsers create a property. If you want to access a non-standard attribute you will need to use the .getAttribute() method.True, but that is, AFAIK, the most reliable way to set attributes on an element created using document.createElement(). I thought there were some cross browser issues with setAttribute. Link to comment Share on other sites More sharing options...
Ingolme Posted May 25, 2012 Share Posted May 25, 2012 True, but that is, AFAIK, the most reliable way to set attributes on an element created using document.createElement(). I thought there were some cross browser issues with setAttribute.The difference appears when attempting to use custom properties and attributes.If you set the property "custom" rather than setting attribute "custom" then, theoretically, it won't show up when printing out the innerHTML of the parent.If you hardcoded an attribute "custom" onto any particular element then, theoretically, you can only access its value using getAttribute(). The idea, anyways, is to know when to manipulate properties and when to manipulate attributes. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now