Drycodez Posted July 17, 2011 Share Posted July 17, 2011 If i want to change the colour of a text, how do i do this? string.fontcolor("red"); The above code works, but how does this works: string.style... what does the "style" word do? Link to comment Share on other sites More sharing options...
chrici Posted July 17, 2011 Share Posted July 17, 2011 string.style.color; This is a property of the string and will (if declared) output the current color of the string. In order to change the color you need to assign the property with a value. For instance: string.style.color = "red"; Link to comment Share on other sites More sharing options...
Ingolme Posted July 17, 2011 Share Posted July 17, 2011 The style object refers to all the CSS properties that are set in the element's style attribute. Link to comment Share on other sites More sharing options...
Drycodez Posted July 17, 2011 Author Share Posted July 17, 2011 The style object refers to all the CSS properties that are set in the element's style attribute.huh? What about those with dash(-)? Eg:background-color Link to comment Share on other sites More sharing options...
jeffman Posted July 17, 2011 Share Posted July 17, 2011 Do some reading. CSS properties that contain a hyphen are accessed in JavaScript using camelCase:var myElement = document.getElementById('test');myElement.backgroundColor = "#000";Why would someone design an interface that allowed you to access some CSS properties but not all properties? That makes no sense. The web is worth $billions, not something designed by children. Link to comment Share on other sites More sharing options...
jeffman Posted July 17, 2011 Share Posted July 17, 2011 Here's a good page to look at: http://www.w3schools.com/jsref/dom_obj_style.asp Link to comment Share on other sites More sharing options...
brucemand Posted July 18, 2011 Share Posted July 18, 2011 Here's a good page to look at: http://www.w3schools.com/jsref/dom_obj_style.asp since the styleObj is all to do with CSS, could one say that CSS is a "subset" of JS in terms of DOM manipulation(?) - not dissimilar to jQuery, in that it has it's own set of delimiters(?) but basically the same syntax.maybe not "JS" but it seems like what we learn as JS is the handling of the DOM(structure); while CSS does the DOM(presentation) and jQuery, as a JS extension, the DOM(behaviour). Link to comment Share on other sites More sharing options...
Ingolme Posted July 18, 2011 Share Posted July 18, 2011 jQuery doesn't do anything that you can't do with Javascript alone, it just makes some tasks easier. jQuery is not part of the native Javascript functionality while the style object is. Link to comment Share on other sites More sharing options...
brucemand Posted July 18, 2011 Share Posted July 18, 2011 jQuery doesn't do anything that you can't do with Javascript alone, it just makes some tasks easier. jQuery is not part of the native Javascript functionality while the style object is.and exactly the same can be said with CSS, right ? Link to comment Share on other sites More sharing options...
Ingolme Posted July 18, 2011 Share Posted July 18, 2011 jQuery is simply a program written with Javascript. jQuery makes accessing elements easier, but in the end it's just using Javascript's native HTML DOM methods which you could use yourself without jQuery if you wanted to.The style object is not programmed with Javascript, it's internal to the browser. You can't create a style object yourself with Javascript, you have to take it from the browser. Link to comment Share on other sites More sharing options...
Synook Posted July 18, 2011 Share Posted July 18, 2011 CSS is a separate styling language. CSS can be manipulated with JavaScript.jQuery is just a bunch of functions written in JavaScript. Link to comment Share on other sites More sharing options...
brucemand Posted July 18, 2011 Share Posted July 18, 2011 jQuery is simply a program written with Javascript. jQuery makes accessing elements easier, but in the end it's just using Javascript's native HTML DOM methods which you could use yourself without jQuery if you wanted to.The style object is not programmed with Javascript, it's internal to the browser. You can't create a style object yourself with Javascript, you have to take it from the browser. ahh yes, got it - it has to be defined (by CSS) before JS can "manipulate" it. Link to comment Share on other sites More sharing options...
jeffman Posted July 18, 2011 Share Posted July 18, 2011 You can use JavaScript to access the style property of any page element. No CSS has to be defined before doing this. Defining style rules in JavaScript is the same as adding/modifying a style attribute in an element's HTML tag.However, because the style property IS different from a CSS stylesheet, you cannot use it to read CSS stylesheet rules. You can set values at any time, and you can read the values that you have set. And if your tag does contain a style attribute, you can read those values also. But the style property does not get you access to stylesheet rules. There are ways to do that, but they are a little complicated. Link to comment Share on other sites More sharing options...
brucemand Posted July 18, 2011 Share Posted July 18, 2011 You can use JavaScript to access the style property of any page element. No CSS has to be defined before doing this. Defining style rules in JavaScript is the same as adding/modifying a style attribute in an element's HTML tag.okay, i did a quick test to confim that, i had a <img> element with no style defined, not even width attribute, and i could change it's width via JS !(i thought that was an error in an earlier piece of code i made, i guess i must have "fixed" it by accident some other way.)However, because the style property IS different from a CSS stylesheet, you cannot use it to read CSS stylesheet rules. You can set values at any time, and you can read the values that you have set. And if your tag does contain a style attribute, you can read those values also. But the style property does not get you access to stylesheet rules. There are ways to do that, but they are a little complicated.i see, "advanced" CSS which i've not yet used to full advantage.am still only making single HTML files with mostly inline CSS, or at the "most" in the <head>.am starting a project which will have multiple HTML files though, that will be when i have to use CSS stylesheets proper. Link to comment Share on other sites More sharing options...
thescientist Posted July 19, 2011 Share Posted July 19, 2011 but really only one sheet, right? Link to comment Share on other sites More sharing options...
brucemand Posted July 19, 2011 Share Posted July 19, 2011 but really only sheet, right?ahh, yes - with the right laxative... a nice clean purge in one go... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.