Enrique García Enrique García - 26 days ago 6
HTML Question

Javascript cant caugh <input> ID, (calling JS function from PHP)

I'm really desperate. I don't know why but Javascript can't catch my input:

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


Is builded to when I submit the PHP detect it and call the JS function:

function ClearFields() {
var e = document.getElementById("mensaje");
e.value='';
}


FULL CODE (can't paste it lol):

http://pastebin.com/J8PpkDUD

My form never reload the page and I can submit a text without reloading, the problem is that the value of textbox is always keeped. That's why I'm trying to use javascript; to clear this textbox when it is submitted.

I get this error in JS console:

(index):24 Uncaught TypeError: Cannot set property 'value' of null(…)


Thanks you!! ^^

Answer

You post to the iframe so, as you said yourself, the page is not reloaded and therefore the ClearFields function isn't executed on submit. Add an onsubmit handler to your form and call ClearFields from that.