Humanity Expired Humanity Expired - 1 month ago 12
HTML Question

Html Textinput in Table Data

Our teacher want us to create a website that will recieve text from the user (textfield) and send it to table data and it need to be in

<form>


When i use
<form>
i input text in textfield the text and submit it the text that i enter is showing in table data at the same time its gone like in the text field i know that this is because of
<form>
when i remove the
<form>
the text is showing in table data.

Here is my code :

<form>
Name: <input action="f1.document.getElementById("first_name");" id="first_name" size="30" type="text" >

<button class="okok" name="myBtn" type="submit" value="Submit Data" onClick="ajax_post();">COMPUTE </button>
</form>

<table>
<tr>

<td style="color:white;" id=f1 > </td>
</tr>
</table>


in script
:

function ajax_post(){




var fn = document.getElementById("first_name").value;
var table = document.getElementById("f1");
table.innerText = fn;
}

Answer

That's because the button is 'submitting' the form, which is like refreshing to page in that it will clear the values.

You have to stop the form submitting, like this:

<form onsubmit="return false;">
Comments