0

On input we have HTML like:

<table>
   <tr>
         <td>
            <img ...>
         </td>
         <td>
            <img ...>
         </td>
         ...
   </tr>
</table>

But in last td:

<td>
    <img ...>
    <img ...>
<td>

Question: How to remove 2nd img? We can use PHP, JS and CSS.

5
  • Do you want to remove one of the img tags dynamically after the page has loaded? Commented Jun 27, 2012 at 15:23
  • What have you tried already? There is a lot of resources on this subject on the internet and on SO already (look at DOM parsers). Commented Jun 27, 2012 at 15:23
  • Where is this HTML coming from? Commented Jun 27, 2012 at 15:24
  • if you can use javascript, you can use jQuery. Using jQuery you can do this in at most 5 lines. Commented Jun 27, 2012 at 15:25
  • 1
    But if you could do it on server it may be best to do it there as you don't have to rely on JavaScript being available on the client. The best approach depends on what you are actually trying to do. We definetly need more info/code here :-) Commented Jun 27, 2012 at 15:27

4 Answers 4

2
$dom = new DOMDocument();
$dom->loadHTML($yourhtmlstring);
$x = new DOMXPath($dom);
foreach($x->query('//td/img[2]') as $node) $node->parentNode->removeChild($node);
echo $dom->saveHTML();
Sign up to request clarification or add additional context in comments.

Comments

0

Use JQuery's remove function

$("table tr:last-child > img:nth-child(2)").remove(); //i think this might work...

JQuery remove

EDIT

Changed above to remove 2nd image not the last

5 Comments

Please, aways provide some citation aside a link. If the link breaks your answer is trashed. PS. I don't downvoted you.
@rcdmk: The link is to the jQuery docs. If that goes down, there are bigger problems.
You should check if there are 2 images first. This will remove an image even if there is only one.
Nope, they can change the site and the link may be broken.
So what are you saying, I should never place links? I now have code to show an example of what I mean by the resource...
0

If your HTML is coming from the HTML page or is PHP generated :
A solution with jQuery :

$(document).ready(function(){
    $.each($("#your_table_id td "),function(){

        if( $(this).find("img").length > 1 ) {
            $(this).find("img:gt(0)").remove();
        }
    });
});

3 Comments

Not quite. this is a DOM element not a jQuery object. That's also not how .has() works.
there is no ID's or classes :(
@user1486054 If this is the only table in your HTML document, you can replace #your_table_id td by table td
-1
<code>
<table class="table">
   <tr>
         <td>
            <img>
         </td>
         <td>
            <img>
         </td>
        <td>
            <img >
            <img >
         </td>
   </tr>
</table>
<script>$(".table tr:last img:last").hide();</script>
</code>

1 Comment

What if the last td has only one image?

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.