Javascript and CSS

0 Jeff Gong · September 15, 2015
I just wanted to know if you could effect a element in html code with javascript like in CSS.
for example, if i wanted to change a p element in html, i would type the p element in the css styles and it would change the p element accordingly, i was wondering i you could do anything like that with javascript?

Post a Reply

Replies

Oldest  Newest  Rating
0 Brotsky Tv · September 25, 2015
hey there !!

Try like this :

<!DOCTYPE html>
<html>
<body>

<p id="p1">Hello World!</p>
<p id="p2">Hello World!</p>

<script>
document.getElementById("p2").style.color = "blue";
document.getElementById("p2").style.fontFamily = "Arial";
document.getElementById("p2").style.fontSize = "larger";
</script>

<p>The paragraph above was changed by a script.</p>

</body>
</html>




Please Visit for more Javascript tutorials : https://www.brotsky.tv/category/javascript/
0 Jeff Gong · September 16, 2015
thanks guys
@Casper Hollemans thanks for the recommendation ill definitely try it out
0 Casper Hollemans · September 16, 2015
I'd use jquery for that, it's basically made for those things, you hvae to import the jquery framework in your HTML file first and then try somthing like this:
$(document).ready(function(){
$('div/id/htmltag').css('color', 'red');
// between the round brackets (.css(so over here)) you put the css code in a special format so instead
// of: color: red; you have to put the css code separated in these symbols ''
});

I'd highly recommend to google a bit about jquery it's very easy to learn and usefully.

-Greets
0 rj m · September 16, 2015
You can make JS code that alters the css of a specific element type, id, etc. directly.

something like (if changing an element based on its assigned id):

function changeCSS {
  document.getElementById('target').style.color = 'blue';
}


Hope that's sort of what you were looking for!
0 rj m · September 16, 2015
You can make JS code that alters the css of a specific element type, id, etc. directly.

something like:

function changeCSS {
var cssToChange = document.getElementById('target');

}
  • 1

Javascript

107,309 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators