1

I want to add html tag without ending tag like that <div class="bottom-widget"> . For that I use jQuery prepend() method but full tag was added by this !

Html Markup -

<div class="widget">

        <h2>this is content 1</h2>
    </div>
</div>

Javascript Code :

$(".widget").prepend('<div class="bottom-widget">');
  • 1
    you are missing `.` in selector `$("widget")`, should be `$(".widget")` – Carsten Løvbo Andersen Sep 13 '17 at 08:25
  • 1
    `$(".widget").prepend($("
    ").addClass("bottom-widget"));`
    – Starfish Sep 13 '17 at 08:26
  • you have an error use `"
    "`
    – N. Ivanov Sep 13 '17 at 08:26
  • Possible duplicate of [.append(), prepend(), .after() and .before()](https://stackoverflow.com/questions/14846506/append-prepend-after-and-before) – Dipak Delvadiya Sep 13 '17 at 08:27
  • 1
    _“I want to add html tag without ending tag”_ - that is not possible using .prepend, or any other such method - you can not append “partial” elements. What method could be appropriate, depends on what you are actually trying to _achieve_ here. – CBroe Sep 13 '17 at 08:28
  • Yes . You able to realize my Question. “I want to add html tag without ending tag” . Is it not possible ???? @CBroe – Mahmudul Hasan Sohag Sep 13 '17 at 08:33
  • It is not possible, because it would not make sense. You are working with the DOM here, not _code_. And just repeating what you want is not an explanation as to _why_ you would want that. So, again: What do you want to _achieve_ here? Describe that, without using the words _“I want to add html tag without ending tag”_ or a variation thereof again. – CBroe Sep 13 '17 at 08:35
  • Thanks everybody . This proble solved using "wrapInner" method :) $(".widget").wrapInner("
    ");
    – Mahmudul Hasan Sohag Sep 13 '17 at 08:37

3 Answers3

1

Perhaps you are looking for wrapInner function.

And you code will be:

$(".wrapInner").wrapInner("<div class='bottom-widget'>");
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
1

I assume you need correct html so after this opening tag you will have another with closing one. For this reason you can use jQuery wrapInner function (http://api.jquery.com/wrapinner/) First extract(or create) the element you want to be wrapped in your bottom-widget element, than create bottom-widget element and insert above-mentioned element into it.

1011sophie
  • 237
  • 3
  • 9
0

$(".widget").prepend("<div class='bottom-widget'></div>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="widget">
        <h2>this is content 1</h2>
</div>