Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTML before end tag in jQuery

Tags:

jquery

I want to add HTML using jQuery between

</font> and </td>

<table>
    <tr>
        <td>
            <span>something</span>
            <font color="#C40404">*</font>
        </td>
    <tr>
</table>
like image 652
Sugumar Venkatesan Avatar asked Feb 03 '16 13:02

Sugumar Venkatesan


People also ask

What is append and prepend in jQuery?

Definition and UsageThe prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.

What is jQuery insertBefore?

The insertBefore() is an inbuilt method in jQuery which is used to insert some HTML content before a specified element. The HTML content will be inserted before each occurrence of the specified element. Syntax: $(content).insertBefore(target)

Which jQuery function adds HTML content outside a selection?

append( function ) A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements. Receives the index position of the element in the set and the old HTML value of the element as arguments.

What does HTML () do in jQuery?

html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.


2 Answers

You can do it using different ways.

  1. append();

    $('table tr:first td:first').append('<span>Text</span>');
    
  2. after();

    $('table tr:first td:first font').after('<span>Text</span>');
    
  3. appendTo();

    $('<span>Text 3</span>').appendTo($('table tr:first td:first'));
    

DEMO

$('table tr:first td:first').append('<span>Text 1</span>');
$('table tr:first td:first font').after('<span> Text 2</span>');
$('<span>Text 3</span>').appendTo($('table tr:first td:first'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
    <tr>
        <td>
            <span>something</span>
            <font color="#C40404">*</font>
        </td>
    <tr>
</table>
like image 179
rrk Avatar answered Oct 05 '22 22:10

rrk


<table>
    <tr>
        <td id='td1'>
            <span>something</span>
            <font color="#C40404">*</font>
        </td>
    <tr>
</table>

<script>

$('#td1').append("whatever you want here");

</script>
like image 41
Phiter Avatar answered Oct 06 '22 00:10

Phiter