How to greet with jQuery? - javascript

I want to do this by typing my name in the input, then pressing the button and an h1 tag will appear below the input: Hello (my name)! I don't know how to solve this.
Here is my html and jQuery code:
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#button').click(function () {
$('body').append('<h1></h1>')
$("h1").append('Hello')//+ my name
})
</script>

You need to get the value from the #Name input. Unless you have a specific reason for doing this in multiple steps, you can append the html as a whole like this:
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#button').click(function () {
if ($('#Name').val()) { //make sure the user entered something
$('body').append(`<h1>Hello ${$('#Name').val()}!</h1>`);
}
});
</script>
The backticks `` are a template literal, they allow you to build strings without a bunch of concatenation.
`Hello ${my_name}!` vs 'Hello ' + my_name + '!'

To get the value of the input field you can use:
$('input#Name').val();
Instead of appending it to the h1 element, you can simply set its text to the wanted value, in your case: .text('hello '+getName).
In case a visitor makes a mistake you only need to modify the inner text of the h1 instead of appending it to the body again.
$('h1').text();
$('#button').click(function () {
let getName = $('input#Name').val();
if(!$('h1').length){
$('body').append('<h1 class="greeting-title"></h1>');
}
$("h1").text('Hello '+getName)//+ my name
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>

Related

How to take an input from a text box and change text

I'm trying to get an input from a text input box to transform a paragraph into what's in the input box. Note that this is not a web page just code run in a Browser.
Here's what I've got so far:
<!DOCTYPE html>
<html>
<form><center>
First name:<br>
<input type="text" name="Firstname" id="JohnnyFunc"></form>
<button type="button" onclick="myFunction">Submit</button>
<script>
function myFunction()
{
document.getElementById("JhonnyOutput"). innerHTML="JohnnyFunc"
}
</script>
<center><p>Hello</p><p id="JhonnyOutput">Johhny!</p></center>
</html>
The onclick attribute should include the parentheses after the function name: onclick="myFunction()"
<form>
<center>First name:<br><input type="text" name="Firstname" id="JohnnyFunc"></center>
</form>
<center><button type="button" onclick="myFunction()">Submit</button></center>
<script>
function myFunction()
{
document.getElementById("JhonnyOutput"). innerHTML="JohnnyFunc"
}
</script>
<center><p>Hello</p><p id="JhonnyOutput">Johhny!</p></center>
You were also missing a closing tag for your first <center> element
Also, to change the text to what's in the text box, you need to get the text from the input element:
function myFunction() {
var newText = document.getElementById("JohnnyFunc").value
document.getElementById("JhonnyOutput").innerHTML = newText
}

HTML input into Javascript variable

I've been attempting to grab text input from HTML and trying to output the text back into HTML but for some reason it does not seem to be working. Does putting input within a form alter the interaction with getElementById or is it possible that the second button usage of formaction is interrupting this?
$(document).ready(function(){
$("#submit").on("click",function(){
var test = document.getElementById("searchInput");
$("#text").html(test);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id = "text">
<h1>test</h1>
</div>
EDIT: I have attempted to change
var test = document.getElementById("searchInput");
to
var test = document.getElementById("searchInput").value;
previously and it is still not working. Is there an interaction that is failing that I am missing? Also I would like to be able to store the input as variable i.e thats why I am purposely putting it into a variable before outputting it.
EDIT2: There wasn't anything particularly wrong about the code other than retrieving the value. I am currently using codepen.io to code and did not load jquery into the pen.
Since you're using jQuery, it will make more sense to use jQuery to obtain the elements' value:
Use this to always replace #text element contents with #searchInput value:
$("#text").html($("#searchInput").val());
Use this to append #searchInput value to #text element:
$("#text").append($("#searchInput").val());
But the whole logic does not make any sense. You are changing an elements' value that it is outside the form you are submitting. Either you should prevent submit button click event from submitting the form, or have the #text element inside the form as an input, textarea or hidden field:
$(document).ready(function(){
$("#submit").on("click",function(e) {
e.preventDefault();
$("#text").html($("#searchInput").val());
})
});
or having the #text element inside the form for submitting:
<form>
<input type="hidden" id="text" name="text">
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
You Using form element, so once you click the button , the form will be reloaded, if you want to display textbox value to the element , once remove the form element.
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
<div id ="text">
<h1 id="welc"></h1>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#submit").on("click",function(){
var test = $('#searchInput').val();
$("h1").html(test);
});
});
</script>
Just Remove tag, and check it.
You need to find the element's value and then set it as HTML.
$("#text").html(test.value);
I have updated your question mate check this one .
$(document).ready(function(){
$("#submit").on("click",function(){
var test = document.getElementById("searchInput").value;
$("#text").html(test);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type ="text" id ="searchInput"><br>
<button id = "submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id = "text">
<h1>test</h1>
</div>
I think this will work for you.
<! doctype HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form>
<input type="text" id="searchInput">
<br>
<button type="button" id="submit">Search</button>
<button formaction="https://en.wikipedia.org/wiki/Special:Random" formtarget="_Blank">Random</button>
</form>
<div id="text">
<h1>test</h1>
</div>
</body>
<script>
$(document).ready(function () {
$("#submit").on("click", function () {
$("#text").html($("#searchInput").val());
})
});
</script>
</html>
Edit your JS code please try:
EDIT:
$(document).ready(function(){
$("#submit").on("click",function(){
var test = $("#searchInput").val();
$("#text").html(test);
})
});

How do i alert the textbox input to a user

If i had a button and an input field. How would i alert whatever is in the input field to the user, when the button is clicked.
Explain your code please.
Make it as simple as possible.
<input type="text" id="input" />
<button onclick="displayEnteredText()">Display</button>
<script>
function displayEnteredText() {
var inputText = document.getElementById("input"); // get the element with id "input" which is the textField
alert(inputText.value); // show the value of the input in alert message
}
</script>
One possible approach:
<!DOCTYPE html>
<html>
<head></head>
<body>
<input id="name" value="">
<input type="button" value="show me the name" onclick="alert(document.getElementById('name').value)">
</body>
</html>
Another possible approach:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function () {
var buttonElement = document.getElementById('button');
buttonElement.addEventListener('click', function() {
alert(document.getElementById('name').value);
});
}
</script>
</head>
<body>
<input id="name" value="">
<input id="button" type="button" value="show me the name">
</body>
</html>
With the second approach you can separate responsabilities, one person can create de html, and another person can focus in create javascript code.
Exists several ways to do this, but with two examples i think is enough in the current context
<body>
<input type="text" name="basicText" id="alertInput">
<button class="alertButton">Click me!</button>
</body>
<script type="text/javascript">
$(".alertButton").click(function(){
var value = $("#alertInput").val();
alert(value + " was entered");
});
</script>
In order to show what you typed in your alert, you need to reference the value inside the textbox. Since jquery is tagged in the post, I used it to get what's in the text box.
You can also try this one
HTML
<input type="button" id="btnclick" style="width:100px" value="Click Me" />
<input type="text" id="txtbox">
JS
$("#btnclick").click(function(){
var txtvalue = $("#txtbox").val();
alert("User enter " + txtvalue);
})
FIDDLE

how to display inner HTML output in text box

I am a beginner html and js programmer and I am trying to display inner HTML output into another TextBox.The code I am using does not give the output in the text box.Can anyone tell me what is wrong with this code?
this is my code
<script>
function my()
{
var a=document.getElementById("text").value;
document.getElementById("demo").innerHTML=a;
}
</script>
<input type="text" id="text" onKeyUp="my()"><br><br>
<span id="demo">
<input type="text" id="demo">
</span>
Any Help is Appreciated...Thanks
You have the same id attribute for both span and your input i.e "demo". Change the span id and try it
The below works:
<script>
function my()
{
var a=document.getElementById("text").value;
document.getElementById("demo").value=a;
}
</script>
<input type="text" id="text" onKeyUp="my()"><br><br>
<span id="demo_span">
<input type="text" id="demo">
</span>

Javascript onClick function - copy text to input value

I need to change the value displayed in a form. For example:
<span id="copy" onClick="addValue()">**Text1**</span>
<span id="copy" onClick="addValue()">Text2</span>
Based from what I click, I want to move it to the input value like this:
<input id="paste" class="text" type="input" name="select" value="**Text1**"/>
Function
<script language="javascript" type="text/javascript">
<!--Hide Javascript
function addValue(){
document.getElementById('paste').value = this.innerText;
}
--> </script>
When I click on Text1 in value fild I get "undefined" instead of "Text1" ...
Give this a go.
You need to pass the element into your function so you can access it's properties to assign them to your form input.
HTML:
<form name="myForm">
<span onclick="copyText(this)" >Text1</span>, <span onclick="copyText(this)" >Text2</span>
<br>
<input name="myField"></input>
</form>
Javascript:
function copyText(element) {
document.myForm.myField.value = element.innerHTML;
}
I'd use:
function addValue(el) {
document.getElementById('paste').value = el.innerText || el.textContent || el.outerText;
}
And set the HTML tag like so:
<span id="copy" onClick="addValue(this)">**Text1**</span>
document.myForm.myField.value = "I'm a different value now!";
Is this what you're looking for?
Form and input should have a "name" property, like:
<form name="myForm">
<input name="myField" />
in the case above.

Categories

Resources