save to localStorage - javascript

i'm curious if its possible to save the text input a user types in the field to localStorage , so in this html example , i'd want to save the innner html from the div#match_player_id , that is created when you click the button
<input type="text" id="contract_year" placeholder="Contract" autocomplete="off">
<input value="Submit" onclick="document.querySelector('#match_player_id').innerHTML = document.querySelector('#contract_year').value" type="button">
<div id="match_player_id"></div>

I recommend you to use a javascript function instead:
<script type="text/javascript">
function updateMatchPlayerId() {
var matchPlayerId = document.querySelector('#contract_year').value;
document.querySelector('#match_player_id').innerHTML = matchPlayerId;
if (typeof Storage !== "undefined") {
localStorage.setItem("matchPlayerId", matchPlayerId);
}
}
</script>
HTML:
<input type="text" id="contract_year" placeholder="Contract" autocomplete="off">
<input value="Submit" onclick="updateMatchPlayerId()" type="button">
<div id="match_player_id"></div>

Firstly, you should really be using unobtrusive event handlers to hook to events instead of the outdated on* event attributes. As you've tagged jQuery in the question this can be done incredibly simply.
From there you can just use localStorage.setItem() to save the value you require, like this:
$('button').click(function(e) {
e.preventDefault();
var contractYear = $('#contract_year').val();
$('#match_player_id').html(contractYear);
localStorage.setItem(contractYear);
});
<input type="text" id="contract_year" placeholder="Contract" autocomplete="off">
<button type="button">Submit</button>
<div id="match_player_id"></div>
Working example

Related

Javascript / HTML Question: Taking an input value from a form, and using this to send user to a URL

Thanks for reading. I'm a novice with Javscript, and have done a lot of searches to try and figure this out... (to no avail)
I'm trying to create a situation where the user inputs a single word on a form. And then when they click a submit button, the website takes the word from the input, appends it on the end of an incomplete URL, and sends them to that completed URL.
It's easy probably easy to see why this doesn't work to some of you. And also, embarassingly, I imagine a completely different approach would be best.
Your advice is appreciated.
<form action="https://vrnaut.neocities.org/" method="get">
<label for="MyForm">Enter One Word:</label>
<input type="text" id= "OneWord" name="OneWord" required>
</form>
<button onclick="myFunction()">Submit</button>
<script>
function myFunction(form) {
var GoHere = form.OneWord.value;
location.replace("https://vrnaut.neocities.org/" + GoHere);
}
</script>
Place the button inside you form
Never listen for buttons click - rather use the FORM's "submit" Event:
const EL_form = document.querySelector("#myForm");
const EL_word = document.querySelector("#OneWord");
EL_form.addEventListener("submit", (ev) => {
ev.preventDefault();
location.replace(EL_form.action + EL_word.value);
});
<form id="myForm" action="https://vrnaut.neocities.org/" method="get">
<label for="MyForm">Enter One Word:</label>
<input type="text" id="OneWord" name="OneWord" required>
<button type="submit">Submit</button>
</form>
Your approach is perfectly fine. All that's needed is a little tweaking.
<form action="https://vrnaut.neocities.org/" method="get">
<label for="MyForm">Enter One Word:</label>
<input type="text" id= "OneWord" name="OneWord" required>
<button onclick="myFunction(this.parentElement)">Submit</button>
</form>
<script>
function myFunction(form) {
var GoHere = form.getElementsByTagName("input")[0];
location.replace("https://vrnaut.neocities.org/" + GoHere.value);
}
</script>

Can't get my simple JavaScript Event Handler working

I am trying to get an event handler on an HTML form. I am just trying t get the simplest thing working, but I just cannot see what I am missing.
It is part of a wider project, but since I cannot get this bit working I have reduced it down the most very basic elements 1 text field and a button to try and see what it is I am missing.
All I want to do is get some text entered and flash up message in a different area on the screen.
The user enters text into the input field (id=owner).
The plan is that when the button (id="entry") is pressed the event handler (function "entry") in the entry.js file should cause a message to display.
I don't want the form to take me to a different place it needs to stay where it is
I just want some form of text to go in the: <div id="feedback" section.
When I can get it working: I intend the create the text from the various text fields that get entered.
I Know that this is beginner stuff & I know that I have reduced this down such that it barely worth thought but I would welcome any input please & thank you.
HTML code is:
<form method="post" action="">
<label for="owner">Input Owner: </label>
<input type="text" id="owner" />
<div id="feedback"></div>
<input type="submit" value="enter" id="entry" />
</form>
<script src="entry.js"></script>
Code for entry.js is:
function entry() {
var elOwner = document.getElementById('owner');
var elMsg = document.getElementByID('feedback');
elMsg.textContent = 'hello';
}
var elEntry = document.getElementById('entry');
elEntry.onsubmit=entry;
I have tried:
Adding in a prevent default:
window.event.preventDefault();
doing this through an event Listener:
elEntry.addEventListener('submit',entry,false);
using innerHTML to post the message:
elMsg.innerHTML = "
At present all that happens is that the pushing submit reloads the page - with no indication of any text being posted anywhere.
One issue is that you have a typo, where getElementById capitalized the D at the end.
Another is that preventDefault() should be called on the form element, not the input.
Here's a working example that corrects those two mistakes.
function entry(event) {
var elOwner = document.getElementById('owner');
var elMsg = document.getElementById('feedback');
elMsg.textContent = 'hello';
event.preventDefault();
}
var entryForm = document.getElementById('entry').form;
entryForm.onsubmit = entry;
<form method="post" action="">
<label for="owner">Input Owner: </label>
<input type="text" id="owner" />
<div id="feedback"></div>
<input type="submit" value="enter" id="entry" />
</form>
I also defined a event parameter for the handler. I don't remember is window.event was ever standardized (it probably was), but I'd prefer the parameter.
Be sure to keep your developer console open so that you can get information on errors that may result from typos.
var elEntry = document.getElementById('entry');
elEntry.addEventListener("click", function(e) {
e.preventDefault();
var elMsg = document.getElementById('feedback');
elMsg.textContent = 'hello';
});
<form method="post" action="">
<label for="owner">Input Owner: </label>
<input type="text" id="owner" />
<div id="feedback"></div>
<input type="submit" value="enter" id="entry" />
</form>

How to access form data nested inside divs (TypeScript)

In a following code I want to access user data written in a form each time user presses an enter key after typing some text data inside an input field in chat-form. Do you have any idea how can I access the following text-data using TypeScript? I have already tried with jQuery but none of the tested code seems to work. I am new to web-dev but very eager to learn new things.
<div id="chat-container">
<div id="search-container">
<input type="text" placeholder="search" />
</div>
<div id="conversation-list">
</div>
<div id="new-message-container">
+
</div>
<div id="chat-title">
</div>
<div id="chat-message-title">
</div>
<div id="chat-form">
<input id="chat-form" type="text" placeholder="Type a message!" />
</div>
</div>
first, you should use a semantic HTML by using form tag instead of div so u can use enter key to handle the submit action. second, it is not an appropriate way to duplicate an id for two different elements because id is a unique identifier for the element. finally here is a simple form and it might be helpful.
HTML:
<form id="my-form">
<input type="text" id="my-input" />
<button type="submit" id="submit-btn">send</button>
</form>
JS:
const formEl = document.getElementById("my-form") as HTMLFormElement;
const inputEl = formEl.querySelector("my-input") as HTMLInputElement;
const submitBtnEl = formEl.querySelector("submit-btn") as HTMLButtonElement;
formEl.addEventListener("submit", e => {
e.preventDefault();
// do what you want
});
inputEl.addEventListener("change", (e:Event|any) => {
console.log(e.target.value)
// do what you want
})
Before the answer: you have duplicated id="chat-form"
<div id="chat-form">
<input id="chat-form"type="text" placeholder="Type a message!"/>
</div>
Example
// select element
const elInput: HTMLInputElement = document.querySelector(`#chat-form-input`)
// add onkeypress listener
document.onkeypress = function (e: any) {
// use e.keyCode
if (e.key === 'Enter') {
// code for enter
console.log(elInput)
console.log(elInput.value)
}
}
<body>
<div id="chat-container">
<div id="search-container">
<input type="text" placeholder="search"/>
</div>
<div id="conversation-list">
</div>
<div id="new-message-container">
+
</div>
<div id="chat-title">
</div>
<div id="chat-message-title">
</div>
<div id="chat-form-container">
<input id="chat-form-input" type="text" placeholder="Type a message!"/>
</div>
</div>
</body>
You should try using a combination of JQuery.
Using this, you should put an id on the input element like so:
<input type="text" id="inputField" placeholder="search"/>
Then query the input field with JQuery. Best practice would suggest to store it in a local variable as well.
let inputFieldText = $("#inputField");
Then test for the value in the text field object as returned from JQuery.
if(inputFieldText.val()){
console.log(inputFieldText.val())
}
For reference, there is also a way to do so with document.getElementById("inputField"). Just link this function to a button that runs on pressing it (such as a "submit" button). Hope this helps!

Is there a way to force the user inside a html text input?

Id like to know if anyone has a suggestion for forcing a user to select a text box with Javascript/Jquery?
Example: user clicks X object on web page. User is forced into a text box in which they should enter information first.
Thanks in advance :)
Yes, what you are looking for is called focus.
Here the examples using jquery and plain js:
function setFocusjquery() {
$('#text-1').focus();
}
function setFocus() {
document.querySelector('#text-2').focus();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="setFocusjquery()">FOCUS jquery</button>
<input type="text" id="text-1" />
<br />
<button onclick="setFocus()">FOCUS JS</button>
<input type="text" id="text-2" />
Use JavaScript focus() method.
HTML:
<button type="button" onclick="focusField()">Click me</button>
...
<input type="text" id="myInput">
JavaScript:
function focusField() {
document.getElementById('myInput').focus();
}
You need to add an event listener to your event trigger object with,
Native JavaScript
let myXObject = document.getElementById('#xObject');
let myTextbox = document.getElementById('#myTextbox');
myXObject.addEventListener('click', function() {
myTextbox.Focus();
});
JQuery
let myXObject = $('#xObject');
let myTextbox = $('#myTextbox');
myXObject.click(function() {
myTextbox.focus();
});

Need to enable button when text is entered in field.

I need to enable my button when I type text into my text box. What am I doing wrong?
Code:
<body>
<script>
function IsEmpty(){
if(input1.value!=null){
sendbutton.enabled==true;
}
}
IsEmpty();
</script>
<input class="draft" name="input1" type="text"/>
<button class="send" id="sendbutton" disabled> Send </button>
<ul class="messages">
</ul>
</body>
Change you JavaScript to:
var input1 = document.getElementById('input1'),
sendbutton = document.getElementById('sendbutton');
function IsEmpty(){
if (input1.value){
sendbutton.removeAttribute('disabled');
} else {
sendbutton.addAttribute('disabled', '');
}
}
input1.onkeyup = IsEmpty;
And HTML:
<input class="draft" id="input1" type="text"/>
<button class="send" id="sendbutton" disabled>Send</button>
DEMO
You probably wanted sendbutton.enabled=true;, with a single =. What you've written checks whether they are equal (false, presumably), and then doesn't do anything with the result.
To modify an element at the DOM you need either to use pure Javascript's functionality, let's say getElementById function, or any other Javascript framework.
document.getElementById('sendbutton')
Then refer to the attribute and change it.
You can also use JQuery which will help you heaps. I mean, using selectors.
Hope that helps,
You need to assign an id to your input element.
<input class="draft" name="input1" id='input1' type="text"/>
and in your javascript access it like this:
if(document.getElementById('input1').value != null || document.getElementById('input1').value!='undefined'){
document.getElementById('sendbutton').disabled=false;
}

Categories

Resources