0

I want to wrap a link which is in a div:

This :

<div id="hello">

http://google.fr/646564897564/8977748946

</div>

will be:

<div id="hello">
<div id="wrap">
http://google.fr/646564897564/8977748946
</div>
</div>
Machavity
  • 30,841
  • 27
  • 92
  • 100
Mathieu
  • 797
  • 2
  • 6
  • 24

2 Answers2

3

have a look here: http://jsfiddle.net/R44pn/

JS

var outer = document.getElementById("hello");
outer.innerHTML = "<div id='wrap'>"+outer.innerHTML+"</div>"

OUTPUT

<div id="hello">
    <div id="wrap">
        http://google.fr/646564897564/8977748946
    </div>
</div>

with JQuery

$("#hello").html("<div id='wrap'>"+$("#hello").html()+"</div>")

fiddle here: http://jsfiddle.net/q27Sw/

hope it helps

BeNdErR
  • 17,471
  • 21
  • 72
  • 103
  • When I try this : its not working : var outer = document.getElementById("url_re"); outer.innerHTML = "" – Mathieu Jun 03 '13 at 15:40
  • Well, I have an url in a page. I want to put a redirection script surround this url in order to redirect the visitor – Mathieu Jun 03 '13 at 15:50
  • you want the user to click that url before he's redirected to the page? – BeNdErR Jun 03 '13 at 15:52
  • Do you think is that possible ? – Mathieu Jun 03 '13 at 16:11
  • have a look here if it helps http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript – BeNdErR Jun 03 '13 at 16:12
3

Making use of jQuery you can use the wrapInner function:

var outer = $("#hello");
outer.wrapInner('<div id="wrap">);

http://jsfiddle.net/R44pn/6/

Alvaro
  • 40,778
  • 30
  • 164
  • 336