I’m kinda new to programming and need help with integrating javascript and html. I am trying to make an html page that has a name input section and a submit button to give a greeting from the function in a javascript file. I’ve tried using the form and input tags and was wondering how i output to the html page.
Thank you in advance.
JavaScript
x
16
16
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6
<title>Test button</title>
7
<script src="./script.js"></script>
8
</head>
9
<body>
10
<form name="testbutton" action="" method="GET">Enter your name</form>
11
<input type="text" name="inputbox" value="">
12
<input type="button" name="button" value="click" onclick="greetings(this.form)" id="">
13
<h1></h1>
14
</body>
15
</html>
16
JavaScript
1
4
1
function greeting(a) {
2
return "Hello " + a + ", nice to meet you"
3
}
4
Advertisement
Answer
Do you want something like this?
JavaScript
1
11
11
1
function alpha(){
2
var str=document.getElementById('input1').value;
3
str ===""? str = "Maria":null;
4
console.log(str);
5
alert(greeting(str));
6
}
7
8
9
function greeting(a) {
10
return "Hello " + a + ", nice to meet you"
11
}
JavaScript
1
2
1
<input type="text" id="input1" />
2
<button onclick="alpha()" value="Maria">Click Me</button>