With this code, a user enters values into various text fields and taps Save to save it to local storage (a persistent kind of HTML web page memory). Tapping Load will load the values back into the fields AND load the values of all the fields into a second textarea field as a summary for export.
I am using the following working code to do two things:
If a value HAS NOT been entered into the STwoDateFrom field in form2a, and the user taps Load, the field would correctly return “undefined” in the field. The if statement says to return a blank field instead. It works. The code also will also enter the field’s caption on a new line in the second field called setupSTwoSummary in form2a.
But if a value HAS been entered into the STwoDateFrom field, the the else statement says to load the value into the original STwoDateFrom field AND enter the field’s caption and value in the second field. This works.
function loadDataS2()
{ "use strict";
var setup2SummariesString = ''; // default is no text added to the second field
var show2Summary = false; // default is don't show the field's value in the second field
setup2SummariesString += "--EVENT--\
Date From: "; // concatenate this to a new line in the second field. "Date From" is the field caption.
if (localStorage["local.storage22.STwoDateFrom"] === undefined) // if no value has been entered in the first field ...
{
document.form2b["STwoDateFrom"].value = ''; // ... then that field should show nothing, not even "undefined"
}
else
{
document.form2b["STwoDateFrom"].value = localStorage["local.storage22.STwoDateFrom"]; // load the value back into the first field
setup2SummariesString += localStorage["local.storage22.STwoDateFrom"]; // also concaenate the value to the second field
show2Summary = true;
}
// write to field
if (show2Summary === true)
{
document.form2a["setupSTwoSummary"].value = setup2SummariesString; // write out concatenated string
}
else
{
document.form2a["setupSTwoSummary"].value = 'No content to display. Enter content in the fields for this setup and tap Save.'; // if no values in any of the fields, write this
}
}
Now I want to adjust the code so that if no value is entered into the first field, I don’t want anything added to the second field either. I tried the following change, but it still shows the new line and the field’s name in the second field if the first field has no value entered. I don’t know what else to try.
Since the contents of setupSTwoSummary is filled with the contents of the variable “setup2SummariesString” (when the variable “show2Summary” is true), if I understand your question correctly you could simply remove the line
[code=javascript]
show2Summary = true;
in the 'else' clause of the first 'if' shown here.
If I misunderstood your dilemma, please try to restate the problem. Maybe draw a little sketch of the flow.
If “true” was removed as you state, then the string would not show at all in the second field if the user had entered a value in the field.
It looks like I need to explain this another way. I just about gave up typing this the first time, fearing it was too complicated to explain. I’ll try to make a diagram. I thought the // comments were more helpful.
I hope this step-by-step guide explains what is happening:
User enters value of “12/27/2011” in the Date From field called STwoDateFrom and does not enter value into the Date To field called STwoDateTo.
User taps Save and values are saved to web memory.
User taps Load and the values are loaded back into STwoDateFrom and STwoDateTo and also into new lines in setupSTwoSummary as:
–EVENT–
Date From: 12/27/2011
Date To:
This is what is happening now with the first code. What I really want to happen is:
–EVENT–
Date From: 12/27/2011
… with Date To: not appearing at all. How do I change the code to effect this? The best, most logical way to me is the second code change, but it doesn’t work!
I can’t see any reference to the ‘Date To’ in the code you have posted. Is there additional code for handling that field? If so then it would help to be able to see that code in order to work out what is happening.
Perhaps a link to the page where you are running the code would help.
The first part of the Javascript that loads the values into the fields looks like:
function loadDataS2()
{ "use strict";
var setup2SummariesString = '';
var show2Summary = false;
setup2SummariesString += "--EVENT--\
Date From: ";
if (localStorage["local.storage22.STwoDateFrom"] === undefined)
{
document.form2b["STwoDateFrom"].value = '';
}
else
{
document.form2b["STwoDateFrom"].value = localStorage["local.storage22.STwoDateFrom"];
setup2SummariesString += localStorage["local.storage22.STwoDateFrom"];
show2Summary = true;
}
//
setup2SummariesString += "";
if (localStorage["local.storage22.STwoDateTo"] === undefined)
{
document.form2b["STwoDateTo"].value = '';
}
else
{
document.form2b["STwoDateTo"].value = localStorage["local.storage22.STwoDateTo"];
setup2SummariesString += "\
Date To: ";
setup2SummariesString += localStorage["local.storage22.STwoDateTo"];
show2Summary = true;
}
//
The second field that gets all the values from all the fields is:
<li><a href="#" data-role="button" data-theme="e" onclick="loadDataS2()">Load</a></li>
<li>Tap on Load to fill this area.
<br>Make any changes, then tap button below to export.<br><br>
<textarea name="setupSTwoSummary" cols="20" rows="7" wrap="hard" class="summaries" placeholder="Tap on Load to fill this area." data-role="none"></textarea>
<input type="submit" value="Send above summary via email"> </li>
</ul>
I hope that helps! This isn’t online; it’s in an app.
This does not work either – "Date From: " should not show if STwoDateFrom has no value, but with the following code it does anyway. I hoped the extra lines directly addressing the setup2SummariesString would prevent the string from appearing:
setup2SummariesString += "--EVENT--";
if (localStorage["local.storage22.STwoDateFrom"] === undefined)
{
document.form2b["STwoDateFrom"].value = '';
setup2SummariesString += ""; // does not prevent the string "date from:" from showing.
show2Summary = false; // 'true' and 'false' do not prevent the string "date from:" from showing.
}
else
{
document.form2b["STwoDateFrom"].value = localStorage["local.storage22.STwoDateFrom"];
setup2SummariesString += "\
Date From: ";
setup2SummariesString += localStorage["local.storage22.STwoDateFrom"];
show2Summary = true;
}