0

I am grabbing text from another site. Once the text is grabbed from another site, I get something like this:

<div id="target">
    <p><a href="http://www.example.com/image.png" rel="nofollow">http://www.example.com/image.png</a></p>
    <p>More Text blah blah blah</p>
    <p><a href="http://www.example.com/image2.png" rel="nofollow">http://www.example.com/image2.png</a></p>
</div>

How can I grab those paragraphs with links to images and replace them with the image itself like by using an img tag so that it replaces all the images it finds with the image itself?

2
  • What do u means "I am grabbing text from another site"? Can I assume the above html elements u described are in your page source? Commented Jan 13, 2017 at 4:30
  • @dimyLute Yes for all intents and purposes the text is in there on page load. Commented Jan 13, 2017 at 4:42

3 Answers 3

2

Try this with jquery.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>

    </title>
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
</head>
<body>
    <div id="target">
    <p><a href="http://www.example.com/image.png" rel="nofollow">http://www.example.com/image.png</a></p>
    <p>More Text blah blah blah</p>
    <p><a href="http://www.example.com/image2.png" rel="nofollow">http://www.example.com/image2.png</a></p>
</div>
	
<script>
    $(document).ready(function(){
        $.each($('#target').find('a'),function(index,element){
            var imgSrc = $(this).attr('href'); ///get url from href property of current 'a' tag
            $(this).parent().remove();///remove parent 'p' tag
            $(this).remove(); ///Remove the current 'a' tag
            $('#target').append('<img src="' +imgSrc  + '" />');
        });
    });
</script>
</body>
</html>

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

2 Comments

This seems to be working, but it adds it to the end of the div rather than replacing the link with the image
As my script in answer it will remove the 'a' tag and its parent 'p' and add an 'img' tag with url value of original href value. Leaving the 'div' tag is just to give a container for the added image tags. You can manipulate as you want to the html elements.
2

Try this:

$(function(){
$('#target a').each(function(index,element){
	var src = $(this).attr('href');
	$(this).parent().append('<img src="' + src  + '" />');
	$(this).remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target">
	<p><a href="http://www.example.com/image.png" rel="nofollow">http://www.example.com/image.png</a></p>
	<p>More Text blah blah blah</p>
	<p><a href="http://www.example.com/image2.png" rel="nofollow">http://www.example.com/image2.png</a></p>
</div>

Comments

0

WorkingFiddle

Create empty var and append image link on desire div.

var html = "";
$('#target >p >a').each(function(i, obj) {
  var imageurl = $(this).prop('href');
  html += '<img src="' + imageurl + '" alt="Mountain View" style="width:304px;height:228px;">';
});
$('#imageShowcase').append(html);

And modified html for demo :

<div id="target">
  <p><a href="http://www.example.com/image.png" rel="nofollow">http://www.example.com/image.png</a></p>
  <p>More Text blah blah blah</p>
  <p><a href="http://www.example.com/image2.png" rel="nofollow">http://www.example.com/image2.png</a></p>
</div>
<div id='imageShowcase'>

</div>

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.