Modifying existing content Adding/Removing content on a page using jQuery.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

HTML Basics Customizing your site using the basics of HTML.
23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
Cascading Style Sheets
XHTML Basics.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
23-Jun-15 HTML. 2 Web pages are HTML HTML stands for HyperText Markup Language Web pages are plain text files, written in HTML Browsers display web pages.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
JQuery: Fun ‘n Games with Selectors. Learning Objectives By the end of this lecture, you should be able to: – Comfortably use jQuery to select based on.
Creating Web Page Forms
Introduction to scripting
Random Stuff Colors Sizes CSS Shortcuts. Learning Objectives By the end of this lecture, you should be able to: – Identify the 3 most common ways in which.
Working with Numbers parseInt() and parseFloat() Math.round() Other Math object functions isNaN() toFixed()
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
XP New Perspectives on Microsoft Access 2002 Tutorial 41 Microsoft Access 2002 Tutorial 4 – Creating Forms and Reports.
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
ITP 104.  While you can do things like this:  Better to use styles instead:
CO1552 – Web Application Development Lists, Special Characters, and Tables.
Selectors. Learning Objectives By the end of this lecture, you should be able to: – Select items using jQuery based on ID, class name, or HTML tag. –
CSS Sprites. What are sprites? In the early days of video games, memory for graphics was very low. So to make things load quickly and make graphics look.
HTML (HyperText Markup Language)
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
Intro to Dreamweaver Web Design Section 7-1 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development I” Course.
Using Html Basics, Text and Links. Objectives  Develop a web page using HTML codes according to specifications and verify that it works prior to submitting.
Chapter 2 HTML Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
XP Mohammad Moizuddin Creating Web Pages with HTML Tutorial 1 1 New Perspectives on Creating Web Pages With HTML Tutorial 1: Developing a Basic Web Page.
Getting Started with HTML Please use speaker notes for additional information!
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
HTML: Hyptertext Markup Language Doman’s Sections.
Chapter 5: Windows and Frames
 HTML is hypertext markup language. It is a way for people to display their information with more complex pictures and text displays. Before HTML, messages.
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
CIS67 Foundations for Creating Web Pages Professor Al Fichera Rev. August 25, 2010—All HTML code brought to XHTML standards.
Return values Efficiency in Coding. Learning Objectives By the end of this lecture, you should be able to: – Be able to apply an ‘object literal’ when.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Web Page Design Forms! Website Design. Objectives What forms can do The Attributes of the form tag Using Textboxes Textareas Checkboxes Radio buttons.
Events Part I Mouse Events. Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate.
Radio Buttons.  Quiz  Radio Buttons  Check boxes 2.
Session 2: Basic HTML HTML Coding Spring 2009 The LIS Web Team Presents.
HTML Forms. A form is simply an area that can contain form fields. Form fields are objects that allow the visitor to enter information - for example text.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
Tutorial #1 Using HTML to Create Web Pages. HTML, XHTML, and CSS HTML – HyperText Markup Language The tags the browser uses to define the content of the.
REEM ALMOTIRI Information Technology Department Majmaah University.
Chapter 4 and 5. Objectives Introduce markup: elements and attributes How browsers interpret HTML documents Basic structure of HTML document What do style.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Moving away from alert() Using innerHTML Using an empty div section
Chapter 6 JavaScript: Introduction to Scripting
Checkboxes, Select boxes, Radio buttons,
Retrieving information from forms
Removing events Stopping an event’s normal behavior
Events Part I Mouse Events.
Adding/Removing content on a page using jQuery
Events Part III The event object.
Modifying HTML attributes and CSS values
Random Stuff Colors Sizes CSS Shortcuts.
Retrieving information from forms
Checkboxes, Select boxes, Radio buttons
Introduction to scripting
Adios alert() Using innerHTML Using an empty section
Presentation transcript:

Modifying existing content Adding/Removing content on a page using jQuery

Learning Objectives By the end of this lecture, you should be able to: – Know when and how to apply the html(), text(), append(), prepend(), before(), after(), remove(), replaceWith() functions. – Appreciate the concept that the key way to learn jQuery (and in fact any programming language) is to “learn how to learn” to use functions. – Look up functions in the API to learn more about them.

Learn how to learn Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. -Maimonidies We will spend a great deal of time in this course going over various functions. However, the objective is absolutely NOT to simply memorize all the details of these various functions. Rather, the idea is that you should start getting comfortable about how and where to LEARN about the various functions available. There are far too many functions in programming languages for any person to learn all of them. Instead, you should be able to go to a reference and look around to see what kinds of functions are available to you, and then how to use them properly. We will discuss more about this as we talk about using the jQuery API.

The html() function “Content”: When we talk about the ‘content’ of a web page, we typically are referring to the information that is seen and heard when a user visits your page. In other words, by ‘content’ we are referring to things like the text visitors read, the images they see, sounds they might hear, and so on. ‘Content’ does not include things such as meta tags, style tags, script code and so on. jQuery give you all kinds of ways of manipulating the content on a page. You can do things like add content, remove content, position content on a page, add/remove tags, modify attributes and so on. The html() function: This function allows you to determine what text is present inside a selection. In addition to allowing you to see what content is present, this function also allows you to modify or even remove that text. Suppose you have the following HTML: Errors Here. You could view the (unparsed) HTML written inside the ‘errors’ div with: alert( $('#errors').html() ); In this case, you would see the following text inside an alert box: Errors Here. Note that you would not see any HTML markp since alert boxes are unable to display markup. If you wanted to view the actual content including HTML markup, you could use document.write() : document.write( $('#errors').html() ); In this case, we would see the text ‘Errors Here.’ in ‘h2’ markup

The html() function contd In addition to allowing you to determine what text is present inside a selection, the html() function also allows you to modify or even remove that text. Errors Here. Using the code above as an example, you could output your own specified HTML content by placing information inside the html() function’s parentheses: $('#errors').html(" Some errors are present. "); This would output ‘Some errors are present’ inside h3 markup. IMPORTANT: Note that when you provide an argument to the html() function as shown in this example, you replace the code that was originally present inside that section. In this case, the code inside the ‘errors’ ID section is replaced by the new content. That is, the content ‘ Errors Here… ” (in h2 markup) would be replaced by “ Some errors are present “ (in h3 markup).

Bug Alert!! Be careful – here is a situation that can be easily missed and lead to headaches when you are trying to work with selections: Suppose you have the following HTML: Errors Here Blah blah blah In this case, the statement: document.write( $('#errors').html() ); Will output ‘Errors Here’ in h2 markup and the words Blah blah blah in h3 markup. However, the statement: document.write( $('#errors h2').html() ); Will output only the words ‘Errors Here’ – and they will NOT be in h2 markup! The reason is that the moment you append the ‘h2’ selector after the ‘#errors’ selector, jQuery only selects inside the h2 tag. In other words, the ‘h2’ tag itself is left out! AS ALWAYS, be sure to try this example out for yourself and experiment with it.

The text() function I should mention that you will probably not use the text() function all that often. However, we will discuss it here to illustrate a couple of concepts. The text() function returns only the text inside the selection and ignores all HTML tags. Suppose you have the following HTML: Errors Here. Blah blah blah. document.write( $('#errors').text() ); Will output: Errors Here. Blah blah blah. WITHOUT any HTML markup.

The text() function contd Recall that if you provide an argument to the html() function as shown here $('#someId').html(' Hello '); then all of the information that was previously inside the #someId section will be replaced by Hello. The text() function works exactly the same way. However, recall that this function ignores HTML markup. What happens in this case, is that the function will strip out any HTML tags and replace them by their escaped characters. So any ‘ ’ symbols by >. In other words, if you only wanted to replace the text inside of the selected region without replacing any of the HTML markup tags and attributes you would use the text() function. Suppose you have the following HTML: Errors Here… Then the following line: $('#errors').text(' Errors are present '); Will replace the content inside ‘ errors ’ with the text: <h2>Errors are present</h2>

The text() function Pop Quiz: Errors Here… Look at the HTML example (in green) above. Do you think the following statement would yield any replacements? $('#container h2').text('Some errors are present'); Answer: This will work. The reason is that the text() function will keep digging inside the ‘ container ’ div, even going through nested divs such as ‘ errors ’ for any ‘h2’ tags. It will then do the required replacement(s).

The append() and prepend() functions These are very useful functions! The prepend() function adds items immediately inside of the selection. You might use it to add text to the beginning of a paragraph, or div section, or list, etc. $('#errors').prepend(" Have a great day! "); Example: Some of my favorite sports: Hockey Soccer Wrestling To add an item to the list: $('#favSports').prepend(" Basket Weaving "); In this case, the code would look inside the ‘ favSports ’ section (i.e. it goes inside the tag) and would add the argument (in this case, the staement) directly inside the ‘favrSports’ section. The net result would be that ‘ Basket Weaving ’ is added as an to the beginning of our list. There is a corresponding append() function that adds HTML content immediately before the END of the selection. $('#footer').append(" Today is: " + Date() + " "); //Note our use of the JS ‘Date()’ function inside the jQuery statement See: append_prepend.htm

Deciding where/when to place ID attributes Some of my favorite sports: Hockey Soccer Wrestling What do you think would happen here? $('#favSports').prepend(" Basket Weaving "); Recall that the prepend function will look inside the ‘ favSports ’ section and and add the text to the very top of that section. In this case, the end result would be to add ‘ Basket Weaving ’ before the words ‘ Some of my favorite sports ’. KEY POINT: You have to be careful exactly where you put your IDs. In this case, because we anticipate modifying the list using scripting (e.g. in response to the user typing new favorite sporst inside text fields or whatnot), your ID should be inside the ‘ul’ tag. On the other hand, if you plan on doing something with the entire section (e.g. showing/hiding it, modifying its appearance, etc), then you should wrap it inside a tag. However, that div tag should have its own separate ID. There is a very important underlying point here: While it is always a good idea to place div tags around key sections of your page, you will often find that you need to be precise in deciding which tags should have an ID attribute. In the example shown here, because we anticipate adding items to the list, then the tag should have its own ID attribute so that we can use our append/prepend functions. Sometimes you will find that you will have to move an ID attribute from one tag to a different one. When to have an ID depends on what you anticipate doing with or doing to that particular section.

The before() and after() functions Very similar to append()/prepend(), however, in this case, the content is added just outside of the selector. This is an important distinction! Example: Hockey Soccer Wrestling $('#favSports').before(" My favorite sports "); //Would place this content just BEFORE the ‘ul’ tag Also very handy in form validation. Suppose you have a required text field such as Name: and the user has left it blank. (We’ll learn how to check for this a little later). You could place an error message just after this text field with: $('#txtName').after(" You forgot to type your name! ");

The remove() function Fairly intuitive… You might use the remove function to hide a pop-up dialog box. Say you have an error message that pops up in a nicely formatted window (as opposed to a boring ‘alert’ box). If the box has an ‘Okay’ button, you can remove the box with the remove() function: Example: //Code to create the box... <input type="button" value="Okay" onclick="$('#error_popup').remove()"

The replaceWith() function Also fairly obvious. However, look at the following code as it also demonstrates that the ‘onclick’ attribute can work with an image: Example: <img src="baseball.jpg" id="baseball" onclick="$('#baseball').replaceWith(' I love this game!! ')"> In this case, when the user clicks on the image, it will be replaced by the h2 text. A reminder about single/double quotes: Generally speaking, you can use either one as long as you are consistent. Just be sure that when you open one set of quotes (whether single or double), you close with the same kind. Also, try to be consistent: I suggest always placing the value of any HTML attribute (e.g. src, href, alt, onclick, type,etc, etc, etc) inside double quotes.In the image example above, note how the values for the src, id, onclick attributes are all inside double quotes.

Using the jQuery API The various functions to manipulate content discussed in this lecture are by no means a complete list. Remember that you should always use a reference that you can trust. This means one that is updated along with the technical standards that you are using. For example, for HTML, you might go to a page maintained by the World Wide Web Consortium (W3C). The W3Schools site, though not perfect, is another site that does a good job of staying on top of the standards. It also give lots of examples. You should absolutely spend time becoming familiar with using the API. Initially, there will be many things you don’t understand. However, over time, you will learn more about the various terms and symbols used in the API. For now, just take a look at these web pages and get a sense of how they work. We will return to the API in more detail periodically throughout the course. The jQuery API can be found at: The jQuery API relating specifically to adding/modifying content on a page can be found at: