0

I want to know how can i add html element before some elements and after e.g: this is code which i added in real file .

<ul class="abcd" id="abcd></ul>

so how can i show it like this ( using javascript );

<div class="blabla">
<div class="ABCD">
   <ul class="abcd" id="abcd></ul>
</div>
</div>
1

3 Answers 3

1
var yourVar; // some  DOM instance
var newFirstElem; //element which should be first in yourVar

yourVar.insertBefore(newFirstElem, yourVar.firstChild);

THis will add as the first child. To add after just use normal append.

Edit:

Consider you have this in the beginning:

  <div class="blabla">
     <div class="ABCD">

     </div>
  </div>

And you want to add <ul class="abcd" id="abcd></ul> to div with class ABCD

$(".ABCD").append('<ul class="abcd" id="abcd></ul>')

You want to add to div with class blabla

$(".blabla").append('<ul class="abcd" id="abcd></ul>')

Consider you have already have this this

<div class="blabla">
         <div class="ABCD">
             <ul class="abcd" id="abcd></ul>
         </div>
      </div>

and now you want to add before the ul inside the div with class abcd

$(".abcd").prepend("<ul class="abcd" id="abcd></ul>");

jQuery makes it simpler. You can just use prepend function. This function allows you to insert whatever you like at the beginning. There are also the option of inserting at specific positions. You should take a look at:

insertAt

insertBefore

after

before

Sign up to request clarification or add additional context in comments.

9 Comments

if you don't mind can you please specify it little more ? i don't have much experience in it . i have same condition i have to add div start before (ul) and end after (ul) there is many code before and after (ul) : // many of html here <ul class="abcd" id="abcd></ul> // some html here
can u specify the before code and after code, so it is simpler to understand.
there is a lots of html . and i have to use this in different pages so every page have different html
if this is not possible to do this id this possible to do it in jquesry-ui.js? because i am getting value of ul from jquery . i want that i could add some javascript in it so it could start a div before ul and end after ul ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"
if you meant to say before and after code ( code which we "will"put after and before using javascript ) then it is a simple div ( div starts before the ul and ends after ul as i said above )
|
0

if you have a html file like this

<html>
<body>
<ul id="1"></ul>
</body>

and you what this

<html>
<head></head>
<body>
    <div>
        <ul id="1"></ul>
    </div>
</body>

use this javascript

var x = document.getElementsByTagName("body")[0];
var b = document.getElementById("1");
var a = document.createElement("div");
x.appendChild(a);
a.appendChild(b);

Comments

0

I hope this is the code you are looking for..

<!doctype>
<html>
    <head>
        <title>Example - Javascript - Add element before/after</title>
        <script language="javascript">
        function mAddBegin()
        {
            var tNew = document.createElement('li');
            tNew.innerHTML = document.getElementById('value').value;

            var tList = document.getElementById('list');
            tList.insertBefore(tNew, tList.firstChild);
        }
        function mAddBefore()
        {
            var tNew = document.createElement('li');
            tNew.innerHTML = document.getElementById('value').value;

            var tList = document.getElementById('list');
            var tTea = document.getElementById('tea');
            tList.insertBefore(tNew, tTea);
        }
        function mAddAfter()
        {
            var tNew = document.createElement('li');
            tNew.innerHTML = document.getElementById('value').value;

            var tList = document.getElementById('list');
            var tTea = document.getElementById('tea');
            tList.insertBefore(tNew, tTea.nextSibling);
        }
        function mAddEnd()
        {
            var tNew = document.createElement('li');
            tNew.innerHTML = document.getElementById('value').value;

            var tList = document.getElementById('list');
            tList.appendChild(tNew);
        }
        </script>
    </head>
    <body>
        <ul id="list">
           <li>Coffee</li>
           <li id="tea">Tea</li>
           <li>Milk</li>
        </ul>
        <input type="text" id="value" />
        <input type="button" onclick="javascript: mAddBegin(); " value="Add begin" />
        <input type="button" onclick="javascript: mAddBefore(); " value="Add before 'Tea'" />
        <input type="button" onclick="javascript: mAddAfter();" value="Add after 'Tea'" />
        <input type="button" onclick="javascript: mAddEnd();" value="Add end" />
    </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.