javascript - Can't catch input ID when calling JS function from PHP -


i don't know why javascript can't catch input:

mensaje: <input id="mensaje" type="text" name="mensaje" value="" style="width:30%;" placeholder="escribe un mensaje enviar...."/> 

is built when submit php detect , call js function:

function clearfields() {     var e = document.getelementbyid("mensaje");     e.value=''; } 

my form never reload page , can submit text without reloading. problem value of textbox kept. that's why i'm trying use javascript, clear textbox when submitted.

i error in js console:

(index):24 uncaught typeerror: cannot set property 'value' of null(…) 

full code:

<html>  <head>     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>     <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=tangerine">      <title>chat de sl</title>     <link rel="icon" href="favicon.ico" type="image/x-icon">     <script type="text/javascript">         $(document).ready(function() {             rsdfsdfsd();             asd();         });          function rsdfsdfsd() {             $('#nax').load('./funcionmensaje.php');             settimeout(rsdfsdfsd, 200);         }          function clearfields() {             var e = document.getelementbyid("mensaje");             e.value = '';         }     </script>     <script>         var contador = 0;          function asd() {             if (contador < 2) {                 contador++;                 var elem = document.getelementbyid('nax');                 elem.scrolltop = elem.scrollheight;                 settimeout(asd, 100);             }         }     </script>     <script>     </script> </head>  <body>     <?php     if(isset($_post['subname'])) {                 echo '<script>',                 'clearfields();',                 '</script>'                 ;          if ($_post['mensaje'] == "") {          }         else {                 file_put_contents('mensajeaenviar', $_post['mensaje']);                 file_put_contents('var', '0');                  $fp = fopen('mensaje.php', 'a');                 $mensaje = $_post['mensaje'].php_eol;                 fwrite($fp, 'tĂș: '.$mensaje .'<br>');             }         }     ?>         <fieldset>             <font color="#c50000">lugar:</font> attached |             <font color="#c50000">avatares cercanos:</font>          </fieldset>          <fieldset>             <legend>chat</legend>              <td>                 <div id="nax" style="height:70%; overflow:auto">                  </div>             </td>         </fieldset>         <style>             nax {                 font-family: 'open sans', serif;                 font-size: 18px;             }              fieldset {                 font-family: sans-serif;                 border-radius: 10px;                 background: #eee;                 margin: 20px;                 padding: 20px;                 box-shadow: 0 0 10px rgba(0, 0, 0, .3);                 position: relative;                 border: 2px groove threedface;             }              legend {                 position: absolute;                 top: -2px;                 background: #fff;                 padding: 10px 20px;                 border: 2px groove threedface;                 border-top: none;                 box-shadow: inset 0 -5px 5px rgba(0, 0, 0, .2);                 text-shadow: 1px 1px 2px rgba(0, 0, 0, .3);                 font-weight: bold;             }              legend span {                 display: block;                 margin: -20px;                 background: #fff;                 padding: 0px 20px;             }              footer {                 font-family: sans-serif;                 margin-top: 2%;                 background: #ddd;                 border-radius: 5px;                 padding: 15px;             }              iframe {                 display: none;             }         </style>          <fieldset style="margin-top:10px;height:3%;">             <form id="formulario" method="post" target="tar">                 mensaje: <input id="mensaje" type="text" name="mensaje" value="" style="width:30%;" placeholder="escribe un mensaje enviar...." />                 <input type='submit' value='enviar' name='subname' />              </form>         </fieldset>         <iframe name="tar" src="localhost/a.php">     </iframe>  </body> <footer>enriquegf utilities</footer>  </html> 

you post iframe so, said yourself, page not reloaded , therefore clearfields function isn't executed on submit. add onsubmit handler form , call clearfields that.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -