Hari Krishna
Hari Krishna

Reputation: 3548

insertAfter is not working

I am trying to add some data after label element. It is working fine with after method, but if i tried the same using inserttAfter method, it is not working. Following is my snippet, can any one tell me, why insertAfter is not working.

<body>
    <label>Java</label>
    <br />
    <label>Python</label>
    <br />
    <label>Perl</label>
    <br />

    <script type="text/javascript">
        var jq = jQuery.noConflict();

        jq(" Programming").insertAfter("label");

        //jq("label").after(" Programing");
    </script>
</body>

Upvotes: 1

Views: 1127

Answers (2)

munjal
munjal

Reputation: 1404

For jq(" Programming"), it will search the HTML tag Programming , but as there is no such tag in your DOM with name and it will return blank. Like this:

jq(" Programming") = []

So you will have to add a tag(or temporary tag to work with insertAfter). Like this:

jQuery("<p> Programming</p>").insertAfter("label");
jQuery("<temp> Programming</temp>").insertAfter("label");

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

The jq('Programming') search for tag Programming since there is no such tag nothing will append. So use insertAfter() only when you need to insert a jQuery element, not for text.


For inserting Programming text after the label use after() method instead.

jq("label").after(" Programming");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Java</label>
<br />
<label>Python</label>
<br />
<label>Perl</label>
<br />

<script type="text/javascript">
  var jq = jQuery.noConflict();
  jq("label").after(" Programing");
</script>

Upvotes: 1

Related Questions