Wednesday, August 13, 2014

Handling Attributes of an Element in JQuery

INTRODUCTION

Attributes define the actual element's style, id etc for any element. For example, element <div> will have attributes like.
  • id
  • style
  • align
  • lang
  • title
Now we will see how can we get the existing value of these attribute and also how to reset the values by the following example.

Example Program

attributes.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Attributes Setter/Getter</title>
</head>
<script type="text/javascript" src="lib/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/attributes.js"></script>

<body>
 <div id="myDiv" style="background: red; width: 200px" align="left" lang="en" title="MySampleDiv">MyDivContent</div>
 <input type="button" id="btn" value="Attributes" />
 <br>
 <br>
 <table border="1">
   <tr>
     <td id="td1"></td>
     <td id="td2"></td>
     <td id="td3"></td>
     <td id="td4"></td>
   </tr>
 </table>

</body>
</html>

attributes.js
$(document).ready(function() {
 $("#btn").click(function() {
   // Get the existing attribute values
   $("#td1").text($("#myDiv").attr("style"));
   $("#td2").text($("#myDiv").attr("align"));
   $("#td3").text($("#myDiv").attr("title"));
   $("#td4").text($("#myDiv").attr("lang"));

   // Reset the existing attribute values
   $("#td1").text($("#myDiv").attr('style', 'background:blue'));
   $("#td2").text($("#myDiv").attr("align", 'right'));
   $("#td2").text($("#myDiv").attr("title", "NewDivValue"));
   $("#td2").text($("#myDiv").attr("lang", "jp"));

 });
});

How to handle styles in JQuery

In this example, we will learn how to add or remove styles in Jquery.

Syntax to Apply Style

$("#elementId").addClass("cssClassName");

Syntax to Remove Style

$("#elementId").removeClass("cssClassName");

Example Program

addRemoveCss.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQuery CSS Example</title>
<link type="text/css" href="styles/addRemoveCss.css" rel="stylesheet" />
</head>
<script type="text/javascript" src="lib/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/addRemoveCss.js"></script>

<body>
 <input type="button" id="changeColorBtn" value="Change Color" />
 <br>
 <br>
 <div id="myDiv" style="width: 375px">Div</div>
</body>
</html>

addRemoveCss.css
@CHARSET "ISO-8859-1";

.redColor {
 background: red;
}

.blueColor {
 background: blue;
}

addRemoveCss.js
$(document).ready(function() {
 $("#changeColorBtn").click(function() {
   $("#myDiv").addClass("redColor");
   alert("redColor added");
   $("#myDiv").addClass("blueColor");
   alert("blueColor added.....");
   $("#myDiv").removeClass("blueColor");
   alert("blueColor removed");
 });
});

How to enable or disable an element using JQuery

Syntax to Enable An Element

$('#submitbutton').prop("disabled", false);

Syntax to Disable An Element

$('#submitbutton').prop("disabled", true);

Example Program

enableDisableElement.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JQuery Example</title>
</head>
<script type="text/javascript" src="lib/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/enableDisableInJQuery.js"></script>

<body>
 <table>
   <tr>
     <td><input type="text" id="myTextBox" value="Hi Dear" /></td>
   </tr>
   <tr>
     <td><input type="button" id="disableTxt" value="Disable Text" /></td>
     <td><input type="button" id="enableTxt" value="Enable Text" /></td>
   </tr>
 </table>
</body>
</html>

enableDisableElement.js
$(document).ready(function() {
 $("#disableTxt").click(function() {
   $("#myTextBox").prop("disabled", true);
 }),

 $("#enableTxt").click(function() {
   $("#myTextBox").prop("disabled", false);
 });
});