PDA

View Full Version : JavaScript being erased by form's GET action



mrpeenut24
September 15th, 2007, 07:21 PM
I'm writing a simple conversion tool using html/js and don't currently have too much experience in javascript. I'm using a form with textboxes and a submit button to initiate the conversion, however, when I push the submit button, the form's GET essentially sends me to a new page and erased the info set by the javascript. Here's what I'm talking about:



<form>
<textarea style="background-color: lightyellow;" name="conversion"></textarea>
<br>
Convert from
<select name="convertFrom">
<option value="text">Text</option>
<option value="hex">Hex</option>
<option value="binary">Binary</option>
</select>
to
<select name="convertTo">
<option value="text">Text</option>
<option value="hex">Hex</option>
<option value="binary">Binary</option>
</select>
<br>
<textarea style="background-color: pink;" name="converted"></textarea>
<br>
<button onclick="action1(conversion, convertFrom, convertTo, converted);">Convert</button>
<br>

</form>
This writes the converted data to the second textarea using
converted.value=convertedData;This writes it to the text area, but as soon as it does, the page is redirected with the GET method and all the data is erased. Is there some postback-ish way I should be doing this?

Thanks.

mrpeenut24
September 17th, 2007, 03:10 PM
I hate bumping my own thread, but google isn't turning anything useful up.

LaRoza
September 17th, 2007, 03:34 PM
<button onclick="action1(conversion, convertFrom, convertTo, converted);">Convert</button>
<br>



Assuming everything else works, add "return false;" to the ECMAScript.

Wybiral
September 17th, 2007, 03:39 PM
You can change the forms "action" to a javascript function and have it process the data without reloading (you'll probably want to learn some DOM control).

LaRoza
September 17th, 2007, 05:12 PM
You can change the forms "action" to a javascript function and have it process the data without reloading (you'll probably want to learn some DOM control).

In a linked file (<script type="text/ecmascript" src="script/script.js"></script>):



window.onload = function()
{
document.getElementsByTagName("form")[0].onclick = function()
{
//code here
return false;
}
}

mrpeenut24
September 17th, 2007, 07:20 PM
Assuming everything else works, add "return false;" to the ECMAScript.

I've got some experience with DOM through using python cgi scripts and a little javascript.

Adding it to the actual function didn't work, but adding it to the button's action did.

I had the form's action set to the function, but I didn't have the return false, so it didn't work.

Anyway, it's working now. Thanks for the help guys.