JQuery Unit-2
JQuery Unit-2
Unit-2
2.1 Getting Setting values from elements
• Getting: Use .val() or .text() to retrieve values from form fields or text
content of elements.
• Setting: Use .val(), .text() or .html() to set values.
Example
<html> // Set new text in the paragraph
<head> $("#setText").click(function() {
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> $("#textElement").text("New Text Content!");
<script> });
$(document).ready(function() { });
</script>
$("#getValue").click(function() {
</head>
var value = $("#inputField").val(); // Get input field
value <body>
<h3>Getting and Setting Values</h3>
alert("Input Value: " + value);
<input type="text" id="inputField" value="Hello,
}); World!">
$("#setValue").click(function() { <button id="getValue">Get Value</button>
$("#inputField").val("New Value!"); // Set new value <button id="setValue">Set Value</button>
}); <br><br>
$("#getText").click(function() { <p id="textElement">This is some text.</p>
alert("Paragraph Text: " + $("#textElement").text()); <button id="getText">Get Text</button>
}); <button id="setText">Set Text</button>
</body>
</html>
2.2 Handling Attributes
<script> <br><br>
<html> <body>
<head> <div id="box">Box</div>
<title>Simple jQuery Example</title> <button id="change">Change</button>
<style> <button id="toggle">Toggle Highlight</button>
#box {
width: 100px; <script>
height: 100px; $(document).ready(function () {
background-color: red;
text-align: center;
// Change color using .css()
line-height: 100px; $("#change").click(function () {
color: white; $("#box").css("background-color", "blue");
} });