#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
jQuery

jQuery HTML

jQuery's HTML methods let you read and change the content and attributes of elements on the page with short, chainable calls. The core ones are .text(), .html() and .val() for content, .attr() and .prop() for attributes and properties, methods like .append() and .remove() for adding and removing elements, and .addClass() and .css() for styling. They work on every element in a selection at once.

Getting and setting content

Most of these methods work as both getter and setter. Called with no argument, they read a value (from the first matched element). Called with an argument, they write it to every matched element.

// Read
const heading = $('#title').text();
const markup = $('#card').html();
const email = $('#email').val();

// Write
$('#title').text('Welcome back');
$('#card').html('<strong>Saved</strong> just now');
$('#email').val('');

// Attributes and properties
$('#logo').attr('alt', 'Company logo');
const link = $('a.docs').attr('href');
$('#agree').prop('checked', true);
const isDisabled = $('#submit').prop('disabled');

attr versus prop

This pair confuses many people. .attr() reads and writes the HTML attribute as written in the markup. .prop() works with the live DOM property. For things that change as the user interacts, such as checked, selected and disabled, use .prop(). $('#agree').attr('checked') tells you whether the box was checked in the original HTML, not whether it is checked now.

Adding and removing elements

const $list = $('#tasks');

$list.append('<li>Last item</li>');      // inside, at the end
$list.prepend('<li>First item</li>');    // inside, at the start
$list.before('<h3>Today</h3>');         // outside, before the list
$list.after('<p>3 tasks left</p>');     // outside, after the list

// Build an element safely from user text
const userText = $('#new-task').val();
$('<li>').text(userText).addClass('task').appendTo($list);

$('.task.done').remove();   // remove elements and their data and events
$list.empty();              // remove only the children

// Classes and inline styles
$('#panel').addClass('open').removeClass('closed');
$('.task').toggleClass('highlight');
$('#banner').css('background-color', '#fff3cd');

Every setter returns the same jQuery object, which is why calls can be chained, as in the class example.

Security mistake: html() with user input

The most dangerous error is passing user-provided text into .html() or .append() as a string, such as $('#comments').append('<li>' + comment + '</li>'). If the comment contains <img src=x onerror=alert(1)>, the browser runs that script. This is a cross-site scripting (XSS) hole. Use .text() for anything a user typed, or build the element first and set its text, as in the safe example above.

A smaller trap: .text() as a getter combines the text of all matched elements, while .html() and .val() return only the first one's value. Loop with .each() if you need each element's value separately.

remove versus empty versus detach

  • .remove() deletes the elements and their jQuery event handlers and data.
  • .empty() keeps the element but deletes everything inside it.
  • .detach() removes the elements but keeps their handlers and data so you can insert them again.

Performance can also suffer when you call .append() inside a long loop, because each call changes the live page. Build the items first, for example in an array of jQuery elements, and append them in one go. Also cache selections you reuse: store $('#tasks') in a variable once instead of searching the DOM again on every line.

Content added with these methods is new to the page, so event handlers bound directly to the old elements do not apply to it. Use delegated events, such as $('#tasks').on('click', '.task', handler), for items you insert later.

Key points

Use .text(), .html() and .val() as getters and setters, .prop() for live states like checked, and the append family to place new content. Never feed raw user input to .html().

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course