Java Script Part 3 V2
Java Script Part 3 V2
Semester 3
IT 3505
Fundamentals of Asynchronous
JavaScript and XML (AJAX) –
Part 3
IT3505 Web Application Development II
jQuery Introduction
})
</script>
<script type="text/javascript">
</script>
$(document).ready(function() {
var objs = $('div'); </body>
alert(objs[0].getAttribute('name')); </html>
alert(objs[1].getAttribute('name'));
})
</script>
Syntax :
.each( callback,arguments)
Semantics:
For each JQuery object call the anonymous function
callback with the specified arguments.
</script>
})
</body>
function printProperties(index,item){ </html>
alert("Object index = "+ index + " name =
"+
item.getAttribute('name'));
}
</script>
<script type="text/javascript">
</script>
$(document).ready(function() {
$('div').each(function(index,item){ </body>
alert("Object index = "+ index + " </html>
name = "+
item.getAttribute('name'));
});
})
</script>
<head> $("#child").each(function(){
var attrs = this.attributes;
<title>JQuery Examples</title>
for(var i=0; i < attrs.length;i++){
<script src="jquery_2.2.0.min.js"></script>
alert("attribute name/value pair of child div =
</head> " + attrs[i].nodeName +","+attrs[i].nodeValue);
<body> }
});
});
<div id="parent" name="name_of_parent"
class="parentClass"> </script>
This is the parent division </body>
<div id="child" name="name_of_child"> </html>
THis is the child division
</div>
</div> Note : $(“#child”) returns a collection of
JQuery objects whereas this returns
an DOM object.
IT3505 Web Application Development II
Listing all attributes of an HTML element
by using only Jquery : Example 2
<!DOCTYPE html> <script type="text/javascript">
<html lang="en"> $(document).ready(function(){
<head> $("#child").each(function(){
<title>JQuery Examples</title> $(this.attributes).each(function(){
<script src="jquery_2.2.0.min.js"></script>
alert("Attribure of div child = " +
</head> this.nodeName +","+this.nodeValue);
<body> });
});
<div id="parent" name="name_of_parent" });
class="parentClass">
This is the parent division </script>
<div id="child" name="name_of_child"> </body>
THis is the child division </html>
</div> Note : $(this.attributes) converts the DOM
</div> object to JQuery object. The each
construct can be used on JQuery objects
not on DOM objects.
IT3505 Web Application Development II
JavaScript Objects
• JavaScript object is a collection of properties.
• A JavaScript object can be defined by using the
following syntax
{property_1, property_2,……., property_n}
• Each property is a name value pair separated
by the : symbol.
Example :
{"name":"Saman","age":20}
Syntax :
for(property in object){
// body
}
</script>
</body>
</html>
$(DOM_Object)
Example:
$(‘.button’) //select all DOM element with the class name
button
Example :
$(‘div .c1’)
- All child elements of div elements with the class
name c1
$(‘div ul .names’)
-all elements with the class name names which are
children of ul elements which in turn are children of
div elements.
Syntax :
addClass(“className1 className2 ……
className_n”)
Syntax:
hasClass(“className”)
Syntax:
removeClass(“className1 className1
className_n”)
$("#div1").removeClass("class1");
}else {
$("#div1").addClass("class1");
}
});
Syntax:
toggleClass(“className1 className1
className_n”)
});