I wish to have the array of inputs to be remembered using the local storage feature of html5. At the moment i can populate the array with weight inputs but once i refresh they disappear. Could anyone help me with this.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Fitness</title>
<link type="text/css" rel="stylesheet" href="styles.css" />
<script>
var arrayX =10;
var arrayY =2;
var array=new Array(arrayX);
var arrayIndex=0;
for (x=0; x<array.length; x++)
{
array [x] = new Array(arrayY);
}
function insert(val1, val2){
if (arrayIndex >= arrayX)
{
alert("Recent Entries is Full");
return false;
}
array[arrayIndex][0]=val1;
array[arrayIndex][1]=val2;
arrayIndex++;
document.getElementById('weight1').value = '';
document.getElementById('unit').value = '';
}
function show() {
var string='<b>Weight Entries</b><br>';
for(i = 0; i < array.length; i++)
{
string+=''+array[i][0]+' '+array[i][1]+'<br>';
}
if(array.length > 0)
document.getElementById('myDiv').innerHTML = string;
}
</script>
</head>
<body>
<header>
<h1>Weight Tracker</h1>
</header>
<article>
<h2>Weight Input</h2>
<p>Please enter your current weight below and submit.</p>
</article>
<form name="form1">
<table align="center" width="407">
<tr>
<td width="154" align="right"><b>Weight</b> </td>
<td width="9"><b> :</b></td>
<td width="224">
<input type="integer" name="weight" id="weight1"/></td>
<tr>
<td width="154" align="right"><b>Unit (KG,Ibs, Stone)</b></td>
<td width="9"><b> :</b></td>
<td width="224">
<input type="integer" name="unit" id="unit"/></td>
</tr>
</table>
</br>
<table width="407">
<input type="button" value="Submit Weight"
onclick="insert(this.form.weight.value,this.form.unit.value);"/>
<input type="button" value="Recent Entries"
onclick="show();"/>
</table>
</form>
</br>
<div id="myDiv"></div>
<nav>
<ul>
<li>home</li>
</ul>
</nav>
just stringify your array and save it to localStorage when you are done.
localStorage["array"] = JSON.stringify(array)
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Template Index</title>
<style>
</style>
</head>
<body>
<div>
<input id="name"/>
</div>
<script>
window.addEventListener("beforeunload", function () {
var name = document.getElementById("name");
localStorage.setItem("name", name.value);
}, false);
window.addEventListener("load", function (){
var name = document.getElementById("name");
name.value = localStorage.getItem("name");
}, false);
</script>
</body>
</html>
the core code is above. you can make your own logic to accomplish your goal
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
Related
When I run the code nothings changes and nothing works, the JavaScript does not do anything not even run the alerts. The ranges in the HTML are supposed to go into a function that gets their value and outputs it to a <div>. That does not happen
This is the JavaScript
$(startup);
function startup(){
$("#speedRange").change(srange);
$("#LimitRange").change(lrange);
$("#ticketbtn").cick(findfine);
$("#ticketreset").click(reset);
}
var speed;
var limit;
function srange() {
speed = $("#speedRange").val();
speed = Number(speed);
$("#YourSpeed").html(speed);
function lrange() {
limit = $("#LimitRange").val();
limit = Number(limit);
$("#OverSpeed").html(limit);
function findfine() {
alert("ll")
}
function reset() {
alert("ll")
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Speeding Ticket Calculator</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Speeding Ticket Calculator</h1>
<table id="speedtable">
<tr>
<td>Select Your Speed<br>40 <input type="range" id="speedRange"> 160 </td>
<td><div id="YourSpeed"></div></td>
</tr>
<tr>
<td>Select The Speed Limit<br>40 <input type="range" id="limitRange"> 100 </td>
<td><div id="OverSpeed"></div></td>
</tr>
</table><br>
<a id="ticketbtn" class="button1">Calculate Penalty</a>
<a id="ticketreset" class="button1">Reset</a><br>
<br><br><br>
<div id="finebox">
</div>
<script src="script.js">
<p id="demo"></p>
</script>
</body>
</html>
This is the Html
Jquery On API to be used in latest version of jquery.
and some function braces where missing,
find the working snippet below.
$(document).ready(function(){
debugger;
startup();
});
function startup(){
debugger;
$("#speedRange").on('change',srange);
$("#limitRange").on('change',lrange);
$("#ticketbtn").on('click',findfine);
$("#ticketreset").on('click',reset);
}
var speed;
var limit;
function srange() {
debugger
speed = $("#speedRange").val();
speed = Number(speed);
$("#yourSpeed").html(speed);
}
function lrange() {
debugger
limit = $("#limitRange").val();
limit = Number(limit);
$("#overSpeed").html(limit);
}
function findfine() {
debugger
alert("ll")
}
function reset() {
debugger;
alert("ll");
$("#overSpeed").html('');
$("#yourSpeed").html('');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Speeding Ticket Calculator</h1>
<table id="speedtable">
<tr>
<td>Select Your Speed<br>40 <input type="range" id="speedRange"> 160 </td>
<td><div id="yourSpeed"></div></td>
</tr>
<tr>
<td>Select The Speed Limit<br>40 <input type="range" id="limitRange"> 100 </td>
<td><div id="overSpeed"></div></td>
</tr>
</table><br>
<a id="ticketbtn" class="button1">Calculate Penalty</a>
<a id="ticketreset" class="button1">Reset</a><br>
<br><br><br>
<div id="finebox">
</div>
<p id="demo"></p>
Hello I am new to website development (started a couple of days ago).
I have some programming experience using Python and C#.
I have been trying to generate an HTML table using JS and apply it a Bootstrap css style.
I can't seem to get it to work.
What I get:
What I want:
I hope to get something that styles like this (simplified from: w3schools)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Basic Table</h2>
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<table class="table">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
</table>
</div>
</body>
</html>
My html page looks like this:
<!DOCTYPE html>
<html>
<head>
<title>CSVParser</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<script type="text/javascript" src="../scripts/csvToHtml.js"></script>
<form onsubmit="return processFile();" action="#" name="myForm" id="aForm" method="POST">
<input type="file" id="myFile" name="myFile"><br>
<input type="submit" name="submitMe" value="Process File">
</form>
<table id="myTable" class="table"></table>
</div>
</body>
</html>
and the JS script is:
function processFile() {
var fileSize = 0;
var theFile = document.getElementById("myFile").files[0];
if (theFile) {
var table = document.getElementById("myTable");
var headerLine = "";
var myReader = new FileReader();
myReader.onload = function(e) {
var content = myReader.result;
var lines = content.split("\r");
for (var count = 0; count < lines.length; count++) {
var row = document.createElement("tr");
var rowContent = lines[count].split(",");
for (var i = 0; i < rowContent.length; i++) {
if (count == 0) {
var cellElement = document.createElement("th");
} else {
var cellElement = document.createElement("td");
}
var cellContent = document.createTextNode(rowContent[i]);
cellElement.appendChild(cellContent);
row.appendChild(cellElement);
}
myTable.appendChild(row);
}
}
myReader.readAsText(theFile);
}
return false;
}
The csv data file I load looks like this:
TestA,TestB,TestC,TestD
Alpha,0.551608445,0.807554763,54.8608682
Beta,0.191220409,0.279946678,57.55254144
This is the generated HTML table from the JS:
<table id="myTable" class="table">
<tr>
<th>TestA</th>
<th>TestB</th>
<th>TestC</th>
<th>TestD</th>
</tr>
<tr>
<td>Alpha</td>
<td>0.551608445</td>
<td>0.807554763</td>
<td>54.8608682</td>
</tr>
<tr>
<td>Beta</td>
<td>0.191220409</td>
<td>0.279946678</td>
<td>57.55254144</td>
</tr>
<tr>
<td></td>
</tr>
</table>
You are missing a <tbody> element around all the <tr> Elements. thats it. if you insert this additionally you are fine with your layout.
I've changed your code quite a bit but here is a working example:
<!DOCTYPE html>
<html>
<head>
<title>CSVParser</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="#" name="myForm" id="aForm" method="POST">
<input type="file" id="myFile" name="myFile"><br>
<input type="button" name="submitMe" value="Process File" onclick="processFile();">
</form>
<table id="myTable" class="table"></table>
</div>
</body>
<script type="text/javascript">
function processFile() {
var fileSize = 0;
var theFile = document.getElementById("myFile").files[0];
if (theFile) {
var table = document.getElementById("myTable");
var headerLine = "";
var myReader = new FileReader();
myReader.onload = function(e) {
var content = myReader.result;
var lines = content.split("\r");
var tbody = document.createElement("tbody");
for (var count = 0; count < lines.length; count++) {
var row = document.createElement("tr");
var rowContent = lines[count].split(",");
for (var i = 0; i < rowContent.length; i++) {
if (count == 0) {
var cellElement = document.createElement("th");
} else {
var cellElement = document.createElement("td");
}
var cellContent = document.createTextNode(rowContent[i]);
cellElement.appendChild(cellContent);
row.appendChild(cellElement);
}
tbody.appendChild(row);
}
table.appendChild(tbody);
}
myReader.readAsText(theFile);
}
return false;
}
</script>
</html>
Working example without having to upload a file
<!DOCTYPE html>
<html>
<head>
<title>CSVParser</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="#" name="myForm" id="aForm" method="POST">
<textarea id="myFile" name="myFile">TestA,TestB,TestC,TestD
Alpha,0.551608445,0.807554763,54.8608682
Beta,0.191220409,0.279946678,57.55254144</textarea><br>
<input type="button" name="submitMe" value="Process File" onclick="processFile();">
</form>
<table id="myTable" class="table"></table>
</div>
</body>
<script type="text/javascript">
function processFile() {
var table = document.querySelector("#myTable");
var content = $("#myFile").val();
var lines = content.split("\n");
var tbody = document.createElement("tbody");
for (var count = 0; count < lines.length; count++) {
var row = document.createElement("tr");
var rowContent = lines[count].split(",");
for (var i = 0; i < rowContent.length; i++) {
if (count == 0) {
var cellElement = document.createElement("th");
} else {
var cellElement = document.createElement("td");
}
var cellContent = document.createTextNode(rowContent[i]);
cellElement.appendChild(cellContent);
row.appendChild(cellElement);
}
tbody.appendChild(row);
}
table.appendChild(tbody);
return false;
}
</script>
</html>
ALSO! You are using myTable.appendChild(row); in your code although you've never initialized any myTable variable.
Load your js script just before the </body> end tag.
Now you are loading the JavaScript before the browser has read all your tags and by then it can't find the elements you are querying in your script.
<body>
<div class="container">
<form onsubmit="return processFile();" action="#" name="myForm" id="aForm" method="POST">
<input type="file" id="myFile" name="myFile"><br>
<input type="submit" name="submitMe" value="Process File">
</form>
<table id="myTable" class="table"></table>
</div>
<!-- Here's the difference -->
<script type="text/javascript" src="../scripts/csvToHtml.js"></script>
</body>
I am trying to get my variable I have saved to display on a table I have created on another page. I get the information from the user from a form, and have a button that is clicked and fires off to save the values into variables. My problem is that I can't change the inner html on the other page with the variable I have saved. I am using 1 js file and 2 html files. I can only use js/jquery, html, and css. here is my code:
loanpage.html
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Super Awesome Loan Guys</title>
<link rel="stylesheet" type="text/css" href="loanpage.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="loanpage.js"></script>
</head>
<body>
<div class="bg" id="text-center">
<div class="companytitle"><span class="dollar">$</span>uper Awe<span class="dollar">$</span>ome Loan Guy<span class="dollar">$</span></div>
<div>
<form action="infopage.html">
<h4>Loan Amount:</h4>
<input type="text" id="loanamount" name="loanamount"><br>
<input type="radio" id="12month" name="time">12 Months
<input type="radio" id="18month" name="time">18 Months
<input type="radio" id="24month" name="time">24 Months
<h4>Name:</h4><input id="namefield" type="text" name="firstlastname">
<h4>Phone:</h4><input id="phonefield" type="text" name="phonennumber">
<h4>Email:</h4><input id="emailfield" type="text" name="email">
<h4>Zip Code:</h4><input id="zipfield" type="text" name="zipcode"><br>
</form>
<button type="button">Submit</button>
</div>
</div>
</body>
</html>
infopage.html
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Super Awesome Loan Guys Loan Information</title>
<link rel="stylesheet" type="text/css" href="loanpage.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="loanpage.js"></script>
</head>
<body>
<div class="bg" id="text-center">
<h1>Here is the info you submitted!</h1>
<table>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>Email Address</th>
<th>Zip Code</th>
<th>Loan Amount</th>
<th>Loan Duration</th>
<th>Interest</th>
</tr>
<tr>
<td id="displayName">1</td>
<td id="displayPhone">1</td>
<td id="displayEmail">1</td>
<td id="displayZip">1</td>
<td id="displayAmount">1</td>
<td id="displayDuration">1</td>
<td id="displayInterest">1</td>
</tr>
</table>
</div>
</body>
</html>
loanpage.js
//js code
var name = "";
var phone="";
var email="";
var zip="";
var loan=0;
var loanrate=12.0;
var loanlen=0;
//Jquery code
$(document).ready(function (){
$("#submitbutton").click(function(){
loan = parseFloat($("#loanamount").val());
if ($("#12month").is(':checked')){
loanlen = 12;
}
else if ($("#18month").is(':checked')){
loanlen = 18;
}
else if ($("#24month").is(':checked')){
loanlen = 24;
}
name = $("#namefield").val();
phone = $("#phonefield").val();
email = $("#emailfield").val();
zip = $("#zipfield").val();
document.getElementById("displayName").innerHTML(name);
document.getElementById("displayPhone").innerHTML(phone);
document.getElementById("displayEmail").innerHTML(email);
document.getElementById("displayZip").innerHTML(zip);
document.getElementById("displayAmount").innerHTML(loan);
document.getElementById("displayDuration").innerHTML(loanlen);
document.getElementById("displayInterest").innerHTML(loanrate);
});
});
Local Storage is your best bet.
// Save data to the current local store
localStorage.setItem("username", "John");
// Access some stored data
alert( "username = " + localStorage.getItem("username"));
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
I have three Pictures on a Website that are generated this way:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<title>AJAX</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="lib/css/stil.css" />
<script type="text/javascript" src="lib/js/ajaxeinsendeaufgabe2.js"></script>
</head>
<body>
<h1>Zusatzinformationen</h1>
<table>
<tr>
<td>
<img class="img" src="img/b1.jpg" />
</td>
<td id="info0"></td>
</tr>
<tr>
<td>
<img class="img" src="img/b2.jpg" />
</td>
<td id="info1"></td>
</tr>
<tr>
<td>
<img class="img" src="img/b3.jpg"/>
</td>
<td id="info2"></td>
</tr>
</table>
</body>
</html>
My Problem: how do i show text when i move with the mouse over one of the three images. Each Image must display unique text.
Here is an incomplete part of the Code:
var resOb = new XMLHttpsRequest ();
window.onload = function () {
document.getElementsByTagName("img").onmouseover =
function sndReq(i){
switch (i) {
case 0:
resOb.open('get', 'info0.txt', true);
break;
case 1:
resOb.open('get', 'info1.txt', true);
break;
case 2:
resOb.open('get', 'info2.txt', true);
break;
resOb.onreadystatechange = function (){
handleResponse (i);
}
How i can generate the text information for each image using onmouseover event?
How to get text from the form and save in text file using javascript
<html >
<head>
<link rel="shortcut icon" type="image/png" href="/favicon1.png" sizes="16x16" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login & Registration System</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><font color="#001c4e" size=18px >
<b>Elvis Login</b></font></td>
</tr>
<tr>
<td><input type="text" id="resultname" name="user_name" placeholder="User ID" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<div id="result"></div>
<script>
// Check browser support
function WriteToFile(f) { //added the form you clicked from
try {
alert("started");
var t = f.user_name.value;
alert(t);
var msg = t;
var fso, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
s = fso.OpenTextFile("C:\\Test\\Logfile.txt", 8, false);
s.writeline(msg);
s.Close();
alert("Comment Saved!");
}
catch (err) {
var strErr = 'Error:';
strErr += '\nNumber:' + err.number;
strErr += '\nDescription:' + err.description;
document.write(strErr);
}
}
$</script>
<tr>
<td><button type="submit" onclick="WriteToFile(this.form)" name="btn-login">Sign In</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
I need get the username from form and save it in the text file.But this code only works in the IE browser.please give suggestions.
what are the things used insteadof new ActiveXObject??
I am not interested to install the plugins.give others suggestions please