Skip to main content

Selecting elements with Jquery | styling html elements with Jquery


We’ll be looking on how to select a HTML element lets say for instance an <h1> tag and change the background color and the font color when clicking it, let’s get started.
To select an element in Jquery we simply pass the element’s name as a string parameter to the $ function (note the $ symbol is the short form for Jquery). Here is how the code will look

Styling elements with jquery




<! DOCTYPE HTML>
</head>
<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
</head>

<body>
<script type="text/javascript">
$(document).ready(function() {
$('h1').click(function(){
$(this).css('background-color','#F00')//setting a background color
$(this).css('color','#fff') // setting font solor
});//click function end
});//document ready function end
</script>

<h1>click me and change the Background</h1>

<style>
h1{ border: 1px solid #999; border-radius:5px; width:510px; cursor: pointer;}
</style>
</body>
</html>

Comments

Popular posts from this blog

Adding a button in HTML | styling a button

To add a button in a HTML page use <input> tags and specify the type as button like so
<input type=”button” value=”submit” action=”submit.php”> this code will be rendered in browser like in Figure 1.1
Further more if you wish to style this button we can apply a set of CSS rules to make the buttons look pretty here are a few examples: Download code for these buttons here



Advanced Jquery easing | Demo and Download

Swing and Jquery easing are included in the Jquery core library but there are many other easing methods which are included in the Jquery easing plugin, available in the Jquery plugin repository. Here are the other easing methods: easeInCirc easeInOutExpo easeOutBack easeOutElastic easeOutBounce easeInOutElastic



to use this easing methods we need to pass its name to the animate function lets see an example which covers most of the above said easing methods.
Download DEMO

<! DOCTYPE HTML> </head> <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="Scripts/easing.js"></script> <style> p{ height:150px; overflow:hidden;} #div1,#div2,#div3,#div4{ position:absolute; border:1px solid #999; border-radius:5px; width:350px; padding:6px;} #div1,#div3{ margin-left:5%; background-color:#00CCFF; } #div2,#div4{ margin-left:50%; background-color:#FC0;} #div3,#div4{ margin-top:…