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>
Buffon
  • 3,613
  • 6
  • 21
  • 14
  • 1
    `i want to replace that html code with javascript code??`. No you don't, sorry. – Robik Apr 13 '11 at 14:54
  • Could you post the actual markup generated, rather than the PHP template? – Matt Ball Apr 13 '11 at 15:04
  • The HTML here seems to cause the browser to implicitly close the `

    ` tag immediately after open rather than wrapping the rest of the markup (see [this](http://jsfiddle.net/muvnQ/) example), at least in Firefox inspected with Firebug.

    – justkt Apr 13 '11 at 15:22

4 Answers4

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

while (i--)
{
    paragraphs[i].innerHTML = 'NotFound';
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

Using Javascript:

var ps = document.getElementsByTagName('p');
for(var i=0, max=ps.length; i<max; i++)
    ps[i].innerHTML = "NotFound";
Vismari
  • 745
  • 3
  • 12
0

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

penguinrob
  • 1,431
  • 3
  • 17
  • 39
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.

Community
  • 1
  • 1
justkt
  • 14,610
  • 8
  • 42
  • 62
  • @justkt : it add the `NotFound` string above the existing not replacing it – Buffon Apr 13 '11 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. – justkt Apr 13 '11 at 15:02
  • Heads up: IDs can not start with a number. – DA. Apr 13 '11 at 15:08
  • 1
    @DA: IDs can start with a number in HTML5. – Matt Ball Apr 13 '11 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](http://jsfiddle.net/muvnQ/)) I reproduced your problem. I inspected it with firebug and the browser is implicitly closing the `

    ` tag right after it is created. Try running your HTML through a validator and cleaning it up. Basically what is happening is that `

    ` does not wrap your contents.

    – justkt Apr 13 '11 at 15:21
  • @justkt : thank u for trying but what i should do as this code was working fine before i want to add some javascript codes – Buffon Apr 13 '11 at 15:35
  • @Buffon - it might appear to work, but it is *not* working. It's not valid. Run your HTML through [the validator](http://validator.w3.org/), clean up errors in the HTML, then try again. – justkt Apr 13 '11 at 15:37
  • @justkt : the vaidator gives me errors which i can't remove it like it gives me an error of using `php` tags i can't remove it or i wont fetch any information from database.. i tried that validator with `http://google.com/` and it also give errors how could that believe anymore?? – Buffon Apr 13 '11 at 15:44
  • @Buffon - run your *output* by the validator - the generated HTML, not the PHP. – justkt Apr 13 '11 at 15:55
  • @justkt : i do that thank u but using the id to that `table` markup itself not `p` thank u very much for trying help – Buffon Apr 13 '11 at 16:12