Skip to main content

Jquery html tag replace



Is there a way to replace, with Jquery, this:







<ul class="sub-menu">

<li><a href="#">Link</a></li>

</ul>







to this:







<div class="under">

<p><a href="#">Link</a></p>

</ul>







Parent element is: <ul id="menu">


Comments

  1. You could unwrap the a element and then wrap it in the new elements:

    $(".sub-menu").find("a").unwrap().unwrap().wrap("<div class='under'>").wrap("<p>");


    The two calls to unwrap remove the li and then the ul, and the two calls to wrap add the div and the p. Note that we have to wrap the div first, then the p.

    Here's a working example (inspect the DOM in the results window to see what you end up with).

    Note that I'm assuming the closing ul was meant to be a closing div tag in your second example!

    Edit

    You could also combine the two calls to wrap, which should be faster:

    $(".sub-menu").find("a").unwrap().unwrap().wrap("<div class='under'><p></p></div>");


    Another edit

    Using unwrap twice seemed a little messy to me. Now that I've had a little spare time I've thrown together a simple plugin that allows you to unwrap multiple ancestor elements from a target element. I've called it unwrapUntil, and you can find it on GitHub. It will work for any number of unwraps, and using it in this specific case would probably be a bit extreme, but it would allow us to do the following:

    $(".sub-menu").find("a").unwrapUntil(2).wrap("<div class='under'><p></p></div>");

    ReplyDelete
  2. you could use the replace with but you would need to build up the html eg:

    $('li').replaceWith('<p><a href="#">Link</a></p>');


    JQuery Ref

    ReplyDelete
  3. var newdiv = $("<div></div>")
    newdiv.addClass("under")
    $(".sub-menu a").each(function(){
    newdiv.append("<p></p>").find("p:last").append(this)
    })
    $(".sub-menu").replaceWith(newdiv)


    http://jsfiddle.net/QLkS5/

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.