web lec 09
web lec 09
Web Engineering
Instructor
•Seemab Karim
•Lecturer at Riphah International University
•Contact Email: [email protected]
Array In JS
• An array is a special variable, which can hold more than
one value in a linear way
Why Use Arrays?
• If you have a list of items (a list of car names, for example), storing the cars in
single variables could look like this:
• let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";
• However, what if you want to loop through the cars and find a specific one? And
what if you had not 3 cars, but 300?
• An array can hold many values under a single name, and you can access the
values by referring to an index number.
Creating an Array
• const array_name = [item1, item2, ...];
• const cars = ["Saab", "Volvo", "BMW"];
OR
function functionName(para..1,para2..)
{
// code to be executed
}
Function Call
• Syntax
• functionName();
JS HTML DOM (Document Object Model)
• With the object model, JavaScript gets all the power it needs to create
dynamic HTML:
• getElementById()
• getElementsByClassName()
• getElementsByTagName()
DOM Selection Methods
getElementById() is a JavaScript DOM method that lets you select a single element on the
web page by its unique id.
What is getElementsByClassName()?
• getElementsByClassName() is a JavaScript DOM method that
lets you select all elements on a web page that share the same
CSS class
• It returns an HTMLCollection (like an array) of all the elements
that match the class name, not just one.
Output
When you
click on button
What is getElementsByClassName()?
• You can also do like this
Output
Output
Output
Syntax:
document.getElementsByTagName("tag-name");
•"tag-name" is the name of the HTML tag (like "p", "h1", "li", etc.)
•It returns a collection of elements, not just one.
Example
Output
Syntax
parentElement.replaceChild(newChild, oldChild);
•parentElement: The parent node that contains the child you want to
replace.
•newChild: The new element that will replace the old one.
•oldChild: The existing child element that will be replaced.
Example
Attribute and Content Manipulation
• element.style (inline styles) is used to modify various types of HTML
elements using JavaScript. This method applies inline styles directly to
elements.
Requirements:
• Check if name is not empty
• Check if email contains @
• Show an alert on success or error
Output
Automatic HTML Form Validation(already discussed)
HTML form validation can be performed automatically by the browser:
If a form field (fname) is empty, the required attribute prevents this form from being
submitted:
DOM Events
Mouse Events
• Click(onclick)
• Double click(ondblclick)
• Right click
• Mouse Hover
• Mouse Out
• Mouse Down
• Mouse Up
Keyboard Events
• Key Press
Window Events
• Resize
• Scroll
• Load
Click event(onclick)
Double click(ondblclick)
• When we double click on hello every one paragraph then the hello() will
call
Right click(oncontextmenu)
Mouse hover(onmouseenter)
• When you just mouse hover the text the alert will
generate
Mouse Out(onmouseout)
• The onmouseout events can be used to trigger a function when the user
mouses out of, an HTML element
Mouse Down(onmousedown)
• Similar to click event when we click on element then
function will call
Mouse up(onmouseup)
• when the mouse-button is released, the onmouseup event is triggered
Onkeypress event
• Just work on body or form tag, and when we click on
any key from keyboard then this event will trigger
Onresize event
• Work on body tag, and When you resize the window
then this event will be trigger
Onscroll event
• When you scroll window then this event trigger