JavaScript: Unbale to get value from form

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



JavaScript: Unbale to get value from form



I was trying to make javascript take value from textbox and alert it, but it always pops up "Undefined". I tried grabing the value of selected items from combobox, but still shows undefined.
When I try using js in simple webpage, it works. Maybe something wrong with my code.



Note: I want the value to popup when "Schedule" button is clicked. Js will take value from txtbox and selected time value. But it does not..



Code: index.php


<!DOCTYPE html>
<html lang='en'>
<head>
<title>Pinterest Grabber</title>
<style>
html, body
width: 100%;
background-color: #DADADA;
color: black;
font-family: georgia, sans-serif, arial;


.container
background-color: white;
width: 95%;
margin: auto;
padding: 10px;
box-shadow: 0px 5px 10px #333;
border: none;
border-radius: 2px;


.setupForm
background-color: #ff9;
margin: 10px;


.pinterestForm
background-color: #ff9;
margin-top: 2%;


.formTxtbox
margin-bottom: 1%;
border: none;
width: 75%;
text-align: center;
font-size: 17px;
height: 30px;
background-color: white;
color: black;
transition-property: box-shadow;
transition-duration: 0.3s;


.formTxtbox:hover
box-shadow: 0px 0px 10px #cab;


.formButton
margin-bottom: 1%;
border: none;
background-color: royalblue;
color: white;
padding: 7px 35px;
font-size: 17px;
transition-property: background-color, box-shadow, color;
transition-duration: 0.3s;


.formButton:hover
background-color: lime;
box-shadow: 5px 5px 5px black;
color: black;
cursor: pointer;


.txtboxSmall
margin-bottom: 1%;
border: none;
width: 50%;
text-align: center;
font-size: 17px;
height: 30px;
background-color: white;
color: black;
transition-property: box-shadow;
transition-duration: 0.3s;


.txtboxSmall:hover
box-shadow: 0px 0px 10px #cab;


.scheduleContainer
background-color: #FF9800;


#scheduleOptions
border: none;
width: 5%;
height: 30px;


</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function()
$('.setupForm').hide();

$("#btnSettingsToggle").click(function()
$(".setupForm").toggle('fast');
);

);

// js
var interval;
var time = document.getElementById('txtScheduleInterval').value;
var timeOptions = document.getElementById("scheduleOptions").value;

function schedule()

alert(time);


</script>
</head>
<body>
<div class="container">
<header>
<h1 align="center">Pinterest Grabber</h1>
</header>

<button class="formButton" id="btnSettingsToggle">Settings</button>

<center>
<form action="setup.php" method="post" class="setupForm">
<h2>Settings</h2>
<input class="formTxtbox" type="text" name="condapath" placeholder="Your Anaconda Path"><br>
<input class="formTxtbox" type="text" name="envname" placeholder="Enviroment Name To Activate"><br>
<input class="formButton" type="submit" value="Save Settings">
</form>
</center>
<?php
$settings = file('settings.txt', FILE_SKIP_EMPTY_LINES);
echo "<b>Working Path:</b> " . $settings[0] . '<br>';
echo "<b>Working Environment:</b> " . $settings[1] . '<br>';
?>

<center>
<form action="pinterest.php" method="post" class="pinterestForm">
<h2 align="left" style="margin-top:1%;margin-left:1%;">Pinterest Single</h2>
<input class="formTxtbox" type="text" name="singleSearchTerm" placeholder="Enter Search Term (EX: old kl)"><br>
<input class="formTxtbox" type="text" name="singleFilename" placeholder="Enter Filename (EX: data.csv)"><br>
<input class="formButton" type="submit" value="Start" name="btnPinSingle">
</form>

<form action="#" method="post" class="pinterestForm">
<h2 align="left" style="margin-top:1%;margin-left:1%;">Pinterest Bulk</h2>
<input class="formTxtbox" type="text" name="listPath" placeholder="Enter List Path (EX: c:folderlist.txt)"><br>
<input class="txtboxSmall" type="text" id="txtScheduleInterval" placeholder="Enter Schedule Time">
<select id="scheduleOptions">
<option value="secs">Seconds</option>
<option value="mins">Minutes</option>
<option value="hrs">Hours</option>
</select>
<br>
<button type="button" class="formButton" onclick="schedule(); return false;">Schedule</button>
<input class="formButton" type="submit" value="Start Now" name="btnPinBulk">
</form>
</center>

</div>
</body>
</html>



Here is the hosted code on JSFiddle: https://jsfiddle.net/Lnt0crs8/1/



Any help would be appreciated.
Thanks.




5 Answers
5



Fetch textbox values in on click function call and try


function schedule()

var time = document.getElementById('txtScheduleInterval').value;
alert(time);





Thanks it worked! As other answers suggested too, I think it happens because the time var is not set when the function is called.
– SimpleCoder
Aug 10 at 10:46





yes, you got it correct
– Suresh Prajapati
Aug 10 at 10:50



The variable time is now set only once, when the page is loaded.


time



It should be set whenever the schedule button is pressed.



So move the line var time = ... to a place within the schedule function:


var time = ...


function schedule()

var time = document.getElementById('txtScheduleInterval').value;
alert(time);





Thanks it worked!
– SimpleCoder
Aug 10 at 10:46



just add the below lines into the javascript schedule function



time = document.getElementById('txtScheduleInterval').value;


time = document.getElementById('txtScheduleInterval').value;



The value of the variable time is getting initialized while the page is getting loaded . While the page is been loaded the value return by document.getElementById('txtScheduleInterval').value is undefined as the input field txtScheduleInterval has no value in it. So while calling the function schedule you have to make sure that the variable time has the current value in txtScheduleInterval.


document.getElementById('txtScheduleInterval').value


undefined



Hope this helps you..





Thanks. It worked.
– SimpleCoder
Aug 11 at 18:35



You put your javascript which contain dom-operation before your html.



When your "var time = document.getElementById('txtScheduleInterval').value;" ran, it could not get your dom as your expectation. So the solution is either put your script after body or do dom-operation in your schedule function





Thats right. I put it in the function and it worked. Thanks for explaining.
– SimpleCoder
Aug 11 at 18:35



fetch the values when the function is called. It should work!


function schedule()

time = document.getElementById('txtScheduleInterval').value;
timeOptions = document.getElementById("scheduleOptions").value;
alert(time);





Thanks. Thats right, it worked!
– SimpleCoder
Aug 11 at 18:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard