0

i want to replace all between <p> tags with a simple word NotFound

<p id='2'>
        <?php
        foreach ($d as $key=>$value) 
        {
                    extract($value);
                            if($key%4==0)
                            {
                                  echo "</tr>";
                                  echo"<tr>";
                            }
                    include('item.php');
        }
        echo"</table>";
        echo"</div>";

   ?>
 </p>

how can i do that using javaScript??

update: i used that code in the javascript area:

<script type="text/javascript">
        window.onload=msg;
        function msg(){
            document.getElementById('1').onclick=clickhandlee;

            //
        }
        function clickhandlee(){
            var ps = document.getElementsByTagName('p');
        for(var i=0, max=ps.length; i<max; i++){
        ps[i].innerHTML = "NotFound";
      }

        }



        </script>

and the same previous code in the <body> tags

<p id='2'>
        <?php
        foreach ($d as $key=>$value) 
        {
                    extract($value);
                            if($key%4==0)
                            {
                                  echo "</tr>";
                                  echo"<tr>";
                            }
                    include('item.php');
          }
        echo"</table>";
        echo"</div>";

   ?>
 </p>

this is the included php template item.php

<td style='border: 0px none ; margin: 0px; padding: 0px; width: 240px;' align='right' valign='top'>
           <div style='margin-bottom: 10px;'>
    <table class='topic' border='0' cellpadding='0' cellspacing='0' align='right'>
    <tbody>
        <tr>
            <td style='background: url(&#039;style/$style/images/top_background.jpg&#039;) no-repeat left center; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous' align='center' height='35'>
                <div style='overflow: hidden; width: 230px;'>
                <a class='link' href='t<?=$id;?>-<?=$name;?>.html'>
                    <?=$name;?> </a>
                </div>
            </td>
        </tr>
        <tr>
            <td align='center' style="height: 197px">
                <a href='count-<?=$id;?>;.html'>
            <img src='<?=$photo;?>' class='image' border='0' width='220' height='170'/></a>
                <div dir='rtl' class='shortdes'><?=$shortdes;?></div>
            </td>
        </tr>
        <tr valign='top'>
            <td style='background: url(&#039;style/$style/images/footer_background.jpg&#039;) no-repeat left center; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; height: 29px;' align='center' height='35'>
            <a class='dlink' href='count-<?=$id;?>.html'>
                <div class='download' style="height: 17px">

                      التحميل : <?=$visits;?>
                    </div></a>

            </td>
        </tr>
    </tbody>
</table>
</div>
</td>
3
  • 1
    i want to replace that html code with javascript code??. No you don't, sorry. Commented Apr 13, 2011 at 14:54
  • Could you post the actual markup generated, rather than the PHP template? Commented Apr 13, 2011 at 15:04
  • The HTML here seems to cause the browser to implicitly close the <p> tag immediately after open rather than wrapping the rest of the markup (see this example), at least in Firefox inspected with Firebug. Commented Apr 13, 2011 at 15:22

4 Answers 4

2
var paragraphs = document.getElementsByTagName('p'),
    i = paragraphs.length;

while (i--)
{
    paragraphs[i].innerHTML = 'NotFound';
}
5
  • @Matt Ball : it add the NotFound string above the existing not replacing it Commented Apr 13, 2011 at 15:00
  • 1
    It added the NotFound string above the existing ______? Commented Apr 13, 2011 at 15:03
  • I was taught to use for loops when you know the number of loops and a while when you don't. Was I taught wrong? Commented Apr 13, 2011 at 15:05
  • 2
    @Ash: it's just a minor optimization that I like to use. It takes a little less characters to write, and a little less time to execute. Commented Apr 13, 2011 at 15:10
  • How does it know when to stop. Is -1 evaluated to false? Commented Apr 13, 2011 at 15:13
1

Using Javascript:

var ps = document.getElementsByTagName('p');
for(var i=0, max=ps.length; i<max; i++)
    ps[i].innerHTML = "NotFound";
4
  • This is identical to the solution that I already posted, but less efficient. Commented Apr 13, 2011 at 14:56
  • it add the NotFound string above the existing not replacing it Commented Apr 13, 2011 at 15:00
  • For your future reference you don't need to use max. Simply for(var i=0; i<ps.Length; i++). Commented Apr 13, 2011 at 15:09
  • 1
    @Ash: it is faster to store the length separately, otherwise it's evaluated at every iteration. Commented Apr 13, 2011 at 15:11
0

Using Javascript

Note comment by user DA - IDs cannot start with a number before HTML 5, so assume that the ID is spelled out (two) instead of the numeral (2) if not using HTML 5.

If it is only for the <p> tag with id=two:

document.getElementById("two").innerHTML = "NotFound";

If it is for all <p> tags:

var paragraphs = document.getElementsByTagName("p");
for(var i = 0; i < paragraphs.length; i++) {
    paragraphs[i].innerHTML = "NotFound";
}

The key is the innerHTML tag of the DOM elements.

10
  • @justkt : it add the NotFound string above the existing not replacing it Commented Apr 13, 2011 at 15:01
  • @Buffon - please update your question to show where and how you are calling this code. innerHTML when applied correctly should do exactly what you want. Commented Apr 13, 2011 at 15:02
  • Heads up: IDs can not start with a number. Commented Apr 13, 2011 at 15:08
  • 1
    @DA: IDs can start with a number in HTML5. Commented Apr 13, 2011 at 15:12
  • @Buffon - that is because your HTML doesn't appear to be liked by the browser. When I tried in jsfiddle (see here) I reproduced your problem. I inspected it with firebug and the browser is implicitly closing the <p> tag right after it is created. Try running your HTML through a validator and cleaning it up. Basically what is happening is that <p> does not wrap your contents. Commented Apr 13, 2011 at 15:21
0

If you're using jquery: $('p').html('NotFound');

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.