0

I need to change this bit of jQuery..

$(function() {

$("#breadcrump").append("<div id='oldsite'>Can't find what you're looking for? Try our old website: <a href='http://www.brooksbarn.co.uk'>Old Website (brooksbarn.co.uk)</a>  or<a href='mailto:info@brooksbarn.co.uk?Subject=Brooksbarn.com Part Enquiry' target='_top'>E-Mail</a></div>");

});

Into regular javascript, I've looked into many ways, but my lack of js knowledge seems to be my stumbling block.

Here is what I've come up with so far:

document.body.onload = addElement;

function addElement () { 
// create a new div element 
// and give it some content 
var newDiv = document.createElement("oldsite"); 
var newContent = document.createTextNode("Can't find what you're looking for? Try our old     website: <a href='http://www.brooksbarn.co.uk'>Old Website (brooksbarn.co.uk)</a>  or<a     href='mailto:info@brooksbarn.co.uk?Subject=Brooksbarn.com Part Enquiry' target='_top'>E-Mail</a>"); 
newDiv.appendChild(newContent); //add the text node to the newly created div. 
// add the newly created element and its content into the DOM 
var currentDiv = document.getElementById("breadcrump"); 
document.body.insertBefore(newDiv, currentDiv); 
}
Brooks
  • 49
  • 1
  • 7

3 Answers3

1

Why do you don't do that :

document.getElementById("breadcrump").innerHTML += "<div id='oldsite'>Can't find what you're looking for? Try our old website: <a href='http://www.brooksbarn.co.uk'>Old Website (brooksbarn.co.uk)</a>  or<a href='mailto:info@brooksbarn.co.uk?Subject=Brooksbarn.com Part Enquiry' target='_top'>E-Mail</a></div>";

?

Maxouhell
  • 686
  • 8
  • 21
0

I think you should add these:

1 You are creating a div, not a oldsite tag

var newDiv = document.createElement("div");

2 You need to set the id

newDiv.id = "oldsite";

3 You are originally appending it:

document.body.insertBefore(newDiv, currentDiv.nextSibling);

http://jsfiddle.net/gf6gna1g/

Michael
  • 3,240
  • 5
  • 21
  • 36
0

There are a couple of things you can do, easiest would be to just set the string as innerHTML

(function(){

    function addElem () {
        var newDiv = document.createElement("div");  //create div
        newDiv.id = "oldsite";  //sets id
        newDiv.innerHTML = "Can't find what you're looking for? Try our old     website: <a href='http://www.brooksbarn.co.uk'>Old Website (brooksbarn.co.uk)</a>  or<a     href='mailto:info@brooksbarn.co.uk?Subject=Brooksbarn.com Part Enquiry' target='_top'>E-Mail</a>";  //add html to new element
        document.getElementById("breadcrump").appendChild(newDiv);  //add it to the page
    }

    if (el.addEventListener) {
        window.addEventListener('load', addElem , false); 
    } else if (el.attachEvent)  {
        el.attachEvent('onload', addElem );
    }

}());
epascarello
  • 185,306
  • 18
  • 175
  • 214
  • Thanks for your comment, for some odd reason the jQuery I have provided works correctly, but your code does not. Could something else be causing errors? – Brooks Nov 13 '14 at 13:38
  • because I did not add the document.ready... http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – epascarello Nov 13 '14 at 13:39