0

I have a string that looks like this:

<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>2/"><img src="domain.com/good1.jpg" alt="001.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>3/"><img src="domain.com/good2.jpg" alt="002.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good3.jpg" alt="003.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good4.jpg" alt="003.jpg"/></a></div><!--nextpage-->

Now I want to remove the ahref link on the last line only and I also want to remove <!--nextpage--> on the last line. The final result should look like this:

<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>2/"><img src="domain.com/good1.jpg" alt="001.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>3/"><img src="domain.com/good2.jpg" alt="002.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good3.jpg" alt="003.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><img src="domain.com/good4.jpg" alt="003.jpg"/></div>

I'm having trouble getting my code to work. Here it is:

var val = '<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>2/"><img src="domain.com/good1.jpg" alt="001.jpg"/></a></div><!--nextpage-->\n<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>3/"><img src="domain.com/good2.jpg" alt="002.jpg"/></a></div><!--nextpage-->\n<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good3.jpg" alt="003.jpg"/></a></div><!--nextpage-->\n<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good4.jpg" alt="003.jpg"/></a></div><!--nextpage-->\n';

var ele = document.createElement('div');
ele.innerHTML = val;
ele.lastChild.remove();
var a = ele.lastChild;
ele.replaceChild(a.firstChild, a);
alert(ele.innerHTML);

Can someone help me fix the code?

1
  • Can you remove the surrounding a tag by editing the actual php file (instead of attempting to remove the tag after the html is already generated)? Commented Aug 16, 2016 at 5:29

2 Answers 2

1
var val = '<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>2/"><img src="domain.com/good1.jpg" alt="001.jpg"/></a></div><!--nextpage--><div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>3/"><img src="domain.com/good2.jpg" alt="002.jpg"/></a></div><!--nextpage--><div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good3.jpg" alt="003.jpg"/></a></div><!--nextpage--><div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good4.jpg" alt="003.jpg"/></a></div><!--nextpage-->';

var ele = document.createElement('div');
ele.innerHTML = val;
var children = ele.children;
var lastDiv = children[children.length - 1];
var a = lastDiv.firstChild;
var img = a.firstChild;
lastDiv.appendChild(img);
a.remove();
var childNodes = ele.childNodes;
var comment = childNodes[childNodes.length - 1];
comment.remove();
alert(ele.innerHTML);
Sign up to request clarification or add additional context in comments.

Comments

0

you can easly do it with jquery. refer below code

<html>
<head>
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">


function removeLine(){

    var ele=$('#main');

    var child =$('#main').children().last();

    var subchild=child.children().first();

    var cnt = subchild.contents();
    subchild.replaceWith(cnt);

    var ele1=$('#main');

    ele1.html(ele1.html().substring(0,ele1.html().lastIndexOf("<!--nextpage-->")));



    console.log(ele1.html());


}


</script>



</head>
<body onload="removeLine()">
<div id="main">
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>2/"><img src="domain.com/good1.jpg" alt="001.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>3/"><img src="domain.com/good2.jpg" alt="002.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good3.jpg" alt="003.jpg"/></a></div><!--nextpage-->
<div class="image-wrap"><a class="ajax-load-next" href="<phpcode><?php echo get_permalink( $post->ID ); ?></phpcode>4/"><img src="domain.com/good4.jpg" alt="003.jpg"/></a></div><!--nextpage-->
</div>
</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.