jQuery HTML Manipulation

The following lists of methods used to manipulate the HTML.

.html(content);

.append(content)

.prepend(content)

.after(content)

.before(content)

example:
				
				
This is div for html manipulation

Demo:
This is div for html manipulation

.text()

.val()

.attr()

.prop()

.remove()

.empty()

.css()

.addClass()

.removeClass()

.removeAttr()

The " removeAttr() " method Remove an attribute from each element in the set of matched elements.

Syntax $(selector).removeAttr(attributeName)
Note:
attributeName:required Specifies one or more attributes to remove. To remove several attributes, separate the attribute names with a space
example:
			    
			    

This is another paragraph.

Demo:

This is another paragraph.

.position()

.each()

.eq()

.is()

.first()

.last()

.length

jQuery.isNumeric( value )

.on()

The " on() " method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.There was an another method called " .live() " for doing the same , but in jquery 1.9 thats now removed.

Syntax $(selector).on(event,data,function)
Note:
event: Required. Specifies one or more events to attach to the elements.
data: Optional. Specifies additional data to pass along to the function
function: Required. Specifies the function to run when the event occurs
example:
			    
			    

Click me!


Demo:

Click me!


.next()

The " next() " method Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector..

Syntax .next([selector])
Note:
selector: Optional. A string containing a selector expression to match elements against.
example:
			    
			    
  • list item 1
  • list item 2
  • list item 3 having class third-item
  • list item 4
  • list item 5
Demo:
  • list item 1
  • list item 2
  • list item 3 having class third-item
  • list item 4
  • list item 5
.prev()

The " prev() " method Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.

Syntax .prev([selector])
Note:
selector: Optional. A string containing a selector expression to match elements against.
example:
			    
			    
  • list item 1
  • list item 2
  • list item 3 having class third-item
  • list item 4
  • list item 5
Demo:
  • list item 1
  • list item 2
  • list item 3 having class third-item
  • list item 4
  • list item 5
.not()

The " not() " method remove elements from the set of matched elements.

Syntax .not(selector)
Note:
selector: A string containing a selector expression to match elements against.
example:
			    
			    

First paragraph

Second paragraph having id : prgh_id

Third paragraph

Demo:

First paragraph

Second paragraph having id : prgh_id

Third paragraph

.parent()

The " parent() " method Get the parent of each element in the current set of matched elements, optionally filtered by a selector.

Syntax .parent([selector])
Note:
selector:optional. A string containing a selector expression to match elements against.
example:
			    
			    

Hello

Hello Again

Demo:

Hello

Hello Again

.submit()

The " submit() " method triggers the submit event, or attaches a function to run when a submit event occurs.

Syntax $(selector).submit(function)
Note:
function:optional. Specifies the function to run when the submit event is triggered.
example:
			    
			    
First name:
Last name:
Demo:
First name:
Last name:
.find()

The " find() " method Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

Syntax .find(selector)
Note:
selector:optional. A string containing a selector expression to match elements against.
example:
			    
			 

Hello, how are you?

Me? I'm good.

Demo:

Hello, how are you?

Me? I'm good.

.insertAfter()

The " insertAfter() " method inserts HTML elements after the selected elements.

Syntax $(content).insertAfter(selector)
Note:
content:Required. Specifies the content to insert (must contain HTML tags).
Note : If content is an existing element, it will be moved from its current position, and inserted after the selected elements.
selector:Required. Specifies where to insert the content.
example:
			    
			    

Existing content

This is a paragraph.

This is another paragraph.

Demo:

Existing content

This is a paragraph.

This is another paragraph.

.insertBefore()

The " insertBefore() " method inserts HTML elements before the selected elements.

Syntax $(content).insertBefore(selector)
Note:
content:Required. Specifies the content to insert (must contain HTML tags).
Note : If content is an existing element, it will be moved from its current position, and inserted before the selected elements.
selector:Required. Specifies where to insert the content.
example:
			    
			    

Existing content

This is a paragraph.

This is another paragraph.

Demo:

Existing content

This is a paragraph.

This is another paragraph.

.parseJSON()

The " parseJSON() " Takes a well-formed JSON string and returns the resulting JavaScript object.

Syntax $.parseJSON(json)
Note:
json:Required. : The JSON string to parse.
Type : String
example:
			    
			    
			
Demo:
JSON.stringify()

The " JSON.stringify() " Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

Syntax JSON.stringify(value [, replacer] [, space])
Note:
value:Required . A JavaScript value, usually an object or array, to be converted.
replacer:Optional. A function or array that transforms the results.
If replacer is a function, JSON.stringify calls the function, passing in the key and value of each member.
If replacer is an array, only members with key values in the array will be converted.
space:Optional. Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
Retrun value:A string that contains the JSON text.
example:
			    
			    
Demo:
Questions
  1. How do I disable/enable a form element ?
  2. Ans - // Disable #x
    $('#x').attr('disabled', true);
    // Enable #x
    $('#x').attr('disabled', false);
  3. Finds all inputs that are not checked and highlights the next sibling span. Notice there is no change when clicking the checkboxes since no click events have been linked.
  4. Ans -
    <div>
    <input type="checkbox" name="a" />
    <span>Mary</span>
    </div>
    <div>
    <input type="checkbox" name="b" />
    <span>lcm</span>
    </div>
    <div>
    <input type="checkbox" name="c" checked="checked" />
    <span>Peter</span>
    </div>
    <script>
    $("input:not(:checked) + span").css("background-color", "yellow");
    $("input").attr("disabled", "disabled");
    </script>
  5. Finds all inputs that don't have the name 'newsletter' and appends text to the span next to it
  6. Ans -
    <div>
    <input type="radio" name="newsletter" value="Hot Fuzz" />
    <span>name is newsletter</span>
    </div>
    <div>
    <input type="radio" value="Cold Fusion" />
    <span>no name</span>
    </div>
    <div>
    <input type="radio" name="accept" value="Evil Plans" />
    <span>name is accept</span>
    </div>
    <script>$('input[name!="newsletter"]').next().append('<b>; not newsletter</b>');</script>
  7. How to count input element which are not checked?
  8. Ans -
    $("input:not(:checked)").length;
    OR
    $("input").not(":checked").length;
  9. How to count input element which are checked ?
  10. Ans -
    $("input:checked").length;
After editing Click here:
Output: