this is most likely a no brainier, but i have Brackets as a codding environment if you will, i think it covers like 30 langues or something crazy like that, and well i understand you need sorts of compilers and what not, but here im trying to add some javascript to some html externally, and i kinda plucked some code off the internet to make sure my test code wouldn't have any issues. any ways the real question is , do i need a compiler of sorts for java, if that's stupid (because i think it can be done with notepad honestly) then please take one sec to look at my code and tell me what i might be doing wrong, because i really try to be simple with my tests
THE HTML
<!DOCTYPE html>
<html>
<head>
<Title>Lets Do Something</Title>
<link rel="stylesheet" href="Index.css"/>
<script src="Index.js"></script>
</head>
<body>
<form>
<input type="text" name="inputbox" value=""><p>
<input type="button" name="button" value="'Click" onclick="testresults(this.form)">
</form>
</body>
</html>
THE JAVASCRIPT
function testResults (form) {
var TestVar = form.inputbox.value;
alert ("You typed: " + TestVar);
}
in fact, Below is the site i got it from, but changed from internal script to external scripting
https://www.javaworld.com/article/2077176/scripting-jvm-languages/using-javascript-and-forms.html
Any help would be great, thanks Bunches.
First of all, this is JavaScript and not JAVA. They are not the same.
In the code you posted, there is a small error that is causing the issue.
Replace onclick="testresults(this.form)" with onclick="testResults(this.form)".
Note that I have camel cased the testResults function name. This should give you the results.
function testResults(form) {
var TestVar = form.inputbox.value;
alert("You typed: " + TestVar);
}
<!DOCTYPE html>
<html>
<head>
<Title>Lets Do Something</Title>
<link rel="stylesheet" href="Index.css" />
<script src="Index.js"></script>
</head>
<body>
<form>
<input type="text" name="inputbox" value="">
<p>
<input type="button" name="button" value="'Click" onclick="testResults(this.form)">
</form>
</body>
</html>
Related
I just finished the W3 Schools certification course on JavaScript.
Can somebody explain why the returnValue() function works for the inField element, but not for the output element.
I'm guessing I have to create a variable and then reference the variable with the output element. Even if that is the solution I'd like to understand the logic behind this necessity.
Any assistance is appreciated.
HTML
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<div id='output'>Output: </div>
<div> Enter a value: <input type="text" id="inField"> </div>
<button onclick="returnValue()" id="submit">Submit</button>
</body>
</html>
JavaScript
function returnValue() {
document.getElementById("inField").value="Test";
document.getElementById("output").value="Test";
}
I'm using this javascript code to get the path of a file, but every time when I run the code, the path appears as "C:\fakepath\file". I think that it is a browser issue (I'm using Chrome), but before changing the browser settings, I wonder if there is any way to do the same procedure without having this error. Is there any javascript library to perform this procedure correctly? How can we fix this error code?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function showFileName() {
var fil = document.getElementById("myFile");
alert(fil.value);
}
</script>
</head>
<body>
<form id="frmMain" name="frmMain">
<input type="file" id="myFile" name="myFile"/>
Show Name
</form>
</body>
</html>
As always, thank you very much for the help. :)
Alright, people of Stack Overflow, I have a question for you: I am in a web design class at my high school and learning ahead of the class since I already knew the first half of the class. I was asked by my teacher if I could teach what I have learned about Javascript and I agreed. However, one of the things she wanted me to teach is not working for me when I try it out on my own. I am trying to do a simple check for a variable that when you input a name into a box, if it is my name or the teacher's name it pulls up a popup that says "welcome" or something like that, and if it is anyone else it says "go away" the only issue is that no matter what I try something in the code is not working. This is a test function that I have at the moment; it is intended to print out the
<!doctype html>
<html>
<head>
<script type="text/javascript">
var name = document.KageForm.User.value;
function validator(){
alert(name);
}
</script>
</head>
<body>
<form name="KageForm">
Username:<input type="text" name="User">
<br/>
Password: <input type="password" name="pass">
<br/>
<input type="button"value="Submit" onclick="validator()" />
</form>
<script type="text/javascript">
</script>
</body>
</html>
Here is the full version of the code that I am trying to get to work:
<!doctype html>
<html>
<head>
<script type="text/javascript">
var name = document.KageForm.User.value;
function validator(){
if(name=="Kage Kaldaka"){
alert("Eeyup")};
else
alert("Nnope");
}
</script>
</head>
<body>
<form name="KageForm">
Username:<input type="text" name="User">
<br/>
Password: <input type="password" name="pass">
<br/>
<input type="button"value="Submit" onclick="validator()" />
</form>
</body>
</html>
You have several issues here:
You have a semicolon after the if statement
You are reading the name value on page load, at a point when the input field hasn't even been added to the page yet and certainly hasn't been filled out by the user yet. You need to read it when the user submits the form, i.e. you need to move the name assignment inside the validator method:
JS:
function validator(){
var name = document.KageForm.User.value;
if(name=="Kage Kaldaka"){
alert("Eeyup");
} else {
alert("Nnope");
}
}
There shouldn't be a semicolon after the if. You also have to put the variable inside the function to make it update:
function validator(){
var name = document.KageForm.User.value;
if(name=="Kage Kaldaka"){
alert("Eeyup")
} else {
alert("Nnope");
}
}
JSFiddle: http://jsfiddle.net/FhCTc/1/
You are setting your variable outside your function:
<script type="text/javascript">
var name = document.KageForm.User.value;
function validator(){
if(name=="Kage Kaldaka"){
alert("Eeyup")};
else
alert("Nnope");
}
</script>
This means var name is set when the page loads, but no other times. Move it into validator().
You also ended your if with a semi-colon, which would cause an error on the else portion.
Besides all the previous answers it is in good manner in JavaScript to use === not ==
I am using IE Tester and this code is not working in IE5, IE7 and IE8,
Please give me a solution
<html>
<head>
<title>IE Tester</title>
<script type="text/javascript">
function functions(data){
alert(data);
}
</script>
</head>
<body>
<input type="button" value="btn1" onclick="javascript:functions('<?php echo "add_new";?>');"/>
<input type="button" value="btn2" onclick="javascript:functions('a');"/>
<input type="button" value="btn3" onclick="functions('a');"/>
</body>
</html>
I just tested the following PHP output in IE8 and it works fine. I'd say you may have found a bug in IETester. But, you might want to remove the "javascript:" part just to see if that helps. Edit: This forum might help as well, it looks like the bug has been reported before.
<html>
<head>
<title>IE Tester</title>
<script type="text/javascript">
function functions(data){
alert(data);
}
</script>
</head>
<body>
<input type="button" value="btn1" onclick="javascript:functions('add_new');"/>
<input type="button" value="btn2" onclick="javascript:functions('a');"/>
<input type="button" value="btn3" onclick="functions('a');"/>
</body>
</html
FYI... I used CodePad to generate the PHP output, then tested with jsFiddle
The javascript is correct, so if none of the buttons are working then the likely reason is that you have de-activated javascript in your browser.
Make sure you escape the quotations in the input onclick event using backslash \"
<input type="button" value="btn1" onclick="javascript:functions('<?php echo \"add_new\";?>');"/>
Radio buttons and JS suck. Ok now that I got that out of my system here is my problem: I finally got Javascript to acknowledge the radio button's value after reading getElementById not playing nice with Radio Buttons
I can alert the value but document.write(); won't work?
Here is my code:
<html>
<head>
<script type="text/javascript">
function getRadioValue() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}
window.onload = function() { alert(getRadioValue()); }
</script>
</head>
<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na" id="draftrequirement_2" />
</body>
</html>
In Firefox 5 and Chrome 12 I see 'na' in both the alert and the document, so the document.write() seems to work in those browsers. The radio input is not present after the window load event, though.
Can I ask you why you are using document.write()? There are many alternatives to manipulating the DOM. From w3schools.com (http://www.w3schools.com/js/js_howto.asp)
Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.
Do not use document.write(). Ever!
Make your life easier and start using jQuery or similar library for manipulating DOM.
If you need to do it with pure javascript only, this should work:
<html>
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.getElementById('draftrequirement_2_message').innerHTML = y;
}
</script>
</head>
<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na" id="draftrequirement_2" />
<div id="draftrequirement_2_message" />
</body>
</html>
<html>
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}
</script>
</head>
<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na" id="draftrequirement_2" />NA
</body>
</html>
Use this it is working