0

Hi, this is probably a trivial question but I'm so new to this I would learn a lot from any help... Why does the content in global variable "str" disappear as soon as I unclick the alertbox? How can I keep it? I put the string in a <div> and one see's the content disappear. here is the code :

<head>
<p>input your text </p>
<div>
<form method="get" id="testform" name="testform" onSubmit="document.str = subCont()">
<textarea name="locs" id="locs" cols="40" rows="5">
...
</textarea><br>
<INPUT TYPE=SUBMIT VALUE="Submit">
</form>
</div>
<div id="test"> </div>
</head>

<script type="text/javascript"> 
// I need the inputed text to stay in this variable
var str =new String();

function subCont(){
document.str=document.testform.locs.value;
document.getElementById("test").innerHTML= document.str;
alert(document.str);
return str;
}
</script>
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by drimacus
0

1. You don't need document.str, just str.
2. Use literals. var str = ''; instead of var str = new String();
In your case, str is going to be of type object as String() is an object where as if you use a literal it will be of type string.

Type | Literal
--------------------
object | {} (example: {name: 'Bob', age: 43, gender: 'male', old: true})
array | [] (example: [0, 1, 2, 3, 4, 5, 6])
string | '' (example: 'Test')
regex | // (example: /^[0-9]{3}$/)
number | 0 (example: 0 or 1 or 12312312, all numbers are floating point)
function| function(){} (example: function(){ alert('test'); }; )

3. setting the str value in the onsubmit does nothing as it will be cleared once the page refreshes.

Edited by stbuchok: n/a

0

Thank you so much for your help. I didn't know about the refreshing bit. I now prevent that somehow and my code works like I want with this (onSubmit returns false):

<head>
    <p>input your text </p>
    <div>
    <form method="get" id="testform" name="testform" onSubmit=" subCont();return false;">
    <textarea name="locs" id="locs" cols="40" rows="5">
    ...
    </textarea><br>
    <INPUT TYPE=SUBMIT VALUE="Submit">
    </form>
    </div>
    <div id="test"> </div>
    </head>
     
    <script type="text/javascript">
    // I need the inputed text to stay in this variable
    var str =new String();
     
    function subCont(){
    str=document.testform.locs.value;
    document.getElementById("test").innerHTML= str;
    }
    </script>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.