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:[email protected]?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:[email protected]?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); 
}
2
  • textNode is text, not html, hence why the link is not a link. Commented Nov 13, 2014 at 13:28
  • document.createElement("oldsite") isn't a div it's <oldsite></oldsite> Commented Nov 13, 2014 at 13:30

3 Answers 3

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:[email protected]?Subject=Brooksbarn.com Part Enquiry' target='_top'>E-Mail</a></div>";

?

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

1 Comment

And you just wiped out any events that may have been attached to the content in breadcrump.
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:[email protected]?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 );
    }

}());

2 Comments

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?
because I did not add the document.ready... stackoverflow.com/questions/799981/…
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/

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.