Fill an Input without selecting it before. HTML / JS? [duplicate] - javascript

This question already has answers here:
How to place the cursor (auto focus) in text box when a page gets loaded without javascript support?
(7 answers)
Closed 5 years ago.
I would like to fill an input without having to select it with my mouse before. Just using javascript and not Jquery .
What I mean is that they user just have ton type his answer and it goes directly into the input. Here is my HTML.
<input id="inputresponse" name="response" type="text" value="" />
I have searched everywhere but I can't find a clue. I guess we should use either keyup or keypress, but I can't find how.
Your help is highly appreciated, thanks !

Use Autofocus on HTML tag as -
<input id="inputresponse" name="response" type="text" autofocus>
Another Way, You can use js also -
window.onload = function() {
var input = document.getElementById("inputresponse").focus();
}
<input id="inputresponse" name="response" type="text">

This should work:
$('#inputresponse').focus();
Just setting this on your page will automatically focus on the input element and anything typed will be added within.

Related

Is there a way to "use" a given input value? [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 3 years ago.
I'm very inexperienced so I'm sorry if this is a stupid question.
I was wondering if it's at all possible to code something on a site that takes an input and "uses" it elsewhere - what I'm going for is an input box that asks for a name, and the name given will appear in place of some sort of "name" variable. For example, if you entered your name as "Bob", something like "Hello (name)" would appear as "Hello Bob".
I haven't really seen anything that gives instruction on how to do this, which makes me wonder if it's even possible.
I've been trying to handle the input box itself, and so far it looks like this:
<body>
<form>
<label for="namebox">Name:</label>
<br>
<input type="text" id="uname" name="name" value="Namehere">
<br>
<button>Submit</button>
</form>
<p id="namebox"></p>
<script>
document.getElementById("namebox").innerHTML = "uname";
</script>
</body>
The output of this currently is being sent to a nonexistent page ending in "/?name=(whatever name value is inputted)".
Also, if this does work, would the change to the "name" variable only occur on the page with the input form, or would it carry to all pages on the site?
Thank you for any help.
<script>
document.getElementById("namebox").innerHTML = "Hello" + document.getElementById("uname").value
</script>

Remove cached searches when using a form [duplicate]

This question already has answers here:
How do you disable browser autocomplete on web form field / input tags?
(101 answers)
Closed 4 years ago.
I'm using this code:
<form action="https://google.com/search" method="get">
<input type="text" name="q" />
To allow me to search using google from a custom homepage. But after every search it seems to be caching my search terms.
When I return to the HTML page when I click on the form it has my previous searches as auto fill drop-down box.
I'd like to disable this function and stop the page from caching.
Thank You
It should be as easy as adding autocomplete="off" to your form element.
<form action="https://google.com/search" method="get" autocomplete="off"></form>

jQuery .change() executes only after click or press button on keyboard [duplicate]

This question already has answers here:
JQuery change not firing until blur
(7 answers)
Closed 5 years ago.
I need help with strange behavior of my javascript/jQuery code.
jQuery .change() of simple text input executes only if i click on page or chrome console or press some key on keyboard, but not when i type text in my text input.
Sure, I expect, that code will be executed after each change in text input, but now this looks like:
I type something in text input
Nothing happens
I click anywhere in the document or press a key, let's say, "tab"
Code runs as expected
Can someone explain me whats going on?
Му code is simple:
$('#search_field').change(function() {
alert("test")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="search_field" class="form-control" id="search_field" placeholder="Search" />
if you want to show on type use on('input')
$('#search_field').on('input',function() {
alert($(this).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="search_field" class="form-control" id="search_field" placeholder="Search" />

Which attribute will stop users changing input data but allow jQuery to modify? [duplicate]

This question already has answers here:
What's the difference between disabled="disabled" and readonly="readonly" for HTML form input fields?
(7 answers)
Closed 7 years ago.
I have seen people recommending
<input name="input" readonly="readonly">
and
<input name="input" disabled="disabled">
But which of these two will:
Stop users changing the value
Allow jQuery to update the value
Use readonly, because disabled will also prevent the field from being submitted.

How to change HTML5 datalist background color for the displayed options [duplicate]

This question already has answers here:
Is there a way to apply a CSS style on HTML5 datalist options?
(6 answers)
Closed 7 years ago.
I am using datalist for my Application .
On click of the Arrow its displaying 3 values (Crust1 , Crust2 and Crust3 )
Could you please tell me how can i change the background color for the displayed values
http://jsfiddle.net/jWaEv/77/
<input type="text" onkeypress="return RestrictVarchar(this,event,15);" id="topcrust-0" list="crustlist" class="crust" placeholder="Add Crust">
<datalist id="crustlist" "="">
<option>Crust1</option>
<option>Crust2</option>
<option>Crust3</option>
</datalist>
<input type="hidden" id="topcrustid-0" value="">
Currently, there's no way to style the <option> tag.
If you really want to achieve this, you will have to build a 'hack' and simulate a dropdown without using <select> tag.
See here : How to style the <option> with only CSS?

Categories