What i need to do is build a variable string in the order (name,mark1,mark2,mark3,mark4) from the following form. Anything ive tried doesnt seem to work properly.
<input type = "button" id = "btn" value= "Add Student Data" onclick="getData(this.form)">
...
<script type="text/javascript">
function getData( form )
{
var temp = [ form.name.value, form.mark1.value, form.mark2.value,
form.mark3.value, form.mark4.value ];
var stringData = temp.join( "\n" );
// ... but what you will do with it now you don't say ...
// as a PURE GUESS:
form.resultdata.value = stringData;
}
</script>
You may temp.join( ) on whatever kind of separator(s) you want.
You could even produce CSV data thus:
Code:
var stringData = '"' + temp.join( '","' ) + '"';
Hard to guess what you really want from such vague specifications.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 02-11-2013 at 08:52 PM..
Ummm...if you RELOAD the page, then how will you remember all the answers?
Unless you use server-side code, of course.
If you try to do it all in the browser, you may run out of space if you store the answer in cookies and almost certainly will if you try to pass via the <form> submit and method="get". Could probably use local storage, if the browser you are using supports it.
I'm not quite sure why you need to reload the page.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
You should avoid giving names or id's to your variables/functions/arguments/forms words which are HTML/JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'click' or 'href' or 'closed' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.
And of course you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, confirm, false, int, null, onload, return, this, void, window, and so on.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Thanks for confirming that. This was an exercise given to me as ive began a course in Javascript. Im surprised they put that in.
Following code seems to do the trick incase anyone else is following.
Code:
<div>
<label for="name">Name</label>
<input type="text" name="nameOne" id="nameOne" size="20" pattern = "[A-Z][a-z]+( [A-Z][a-z]+)?" title="Please enter a name (Surname optional) with First Letter capitalised" onblur="isFilled(this)">
</div>
Code:
var temp = [this.nameOne.value, this.mark1.value, this.mark2.value, this.mark3.value, this.mark4.value ];
var stringData = temp.join( "\n" );
alert(temp);
Thanks for the help. This forum will come in real handy for learning code.