I'm trying to find a way to trigger JavaScript when writing on an input
Thanks to everyone can help me
<input id="tophead-searchbar" class="searchbar" placeholder="Cerca" onfocus="mostraanteprimericerca();" onchange="Result1InnerHTML();">
Anyone that can help me?
function Result1InnerHTML() {
document.getElementById("Result1").innerHTML = "Cerca" + document.getElementById("tophead-searchbar").innerHTML + "su Nevent";
}
This is the function I want to call
To get the input as the user types, use the input event. This will also catch content which the user pastes in using
the mouse.
Also note that using onX attributes in your HTML is no longer good practice and should be avoided. Use unobtrusive event handlers attached within your JS code. This can be done using the addEventListsner() method.
Finally, input elements don't have any innerHTML, you need to read the value of the control instead.
const input = document.querySelector('#tophead-searchbar');
const output = document.querySelector('#result');
input.addEventListener('input', e => output.innerHTML = `Cerca ${input.value} su Nevent`);
<input id="tophead-searchbar" class="searchbar" placeholder="Cerca" />
<div id="result"></div>
You can use oninput event instead of onchange
Related
I was wondering if there is any way to make a bootstrap textbox that only accepts one word, or if not any way to show an error message if more than one word is put into the text box.
You could use simple regexp like this /^\w+$/ to check if the value is only one word. To check the value, bind to onkeyup or onchange event on that input:
var input = document.getElementById('text');
var error = document.getElementById('error');
input.onkeyup = function() {
if (!input.value.match(/^\w+$/)) {
error.innerText = 'fill in only one word!';
} else {
error.innerText = '';
}
};
<input type="text" id="text">
<div id="error"></div>
no, there isnt.
if you want it, I would suggest you to use the keyup event and take your event.target.value.trim().split(' '), if more than 1 position, then it has more than 1 word.
You could handle the value when it changes.
$(document).ready(function() {
$('#txt-one').change(function() {
if (/ /g.test($.trim(this.value))) {
console.log('You have entered more than one word!')
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input id="txt-one" type="text" class="form-control" />
Well bootstrap doesn't provide this functionality by default but you can write a small script for it. Please refer the link below, I have written a small script it should be able to give you a basic idea of implementation.
JS BIN Code
https://jsbin.com/duzevuleye/edit?html,output
I am very new to html, css, and javascript so please go easy on me. I am working on an activity that requests: Register the updateCount event handler to handle input changes for the textarea tag. Note: The function counts the number of characters in the textarea.
The Javascript so far is as follows -
var textareaElement = document.getElementById("userName");
function updateCount(event) {
document.getElementById("stringLength").innerHTML = event.target.value.length;
}
// Write code here
I have absolutely no idea what it is asking of me for this problem. Both online resources and textbooks have not been very helpful.
The HTML cannot be changed in any way, forcing me to solve it with just changes the the javascript.
The HTML is as follows -
<label for="userName">User name:</label>
<textarea id="userName" cols="40" rows="3"></textarea><br>
<p id="stringLength">0</p>
Any help would be much appreciated, I'm just trying to learn.
Try this. Add onkeyup event on the <textarea> tag then replace event.target to textareaElement to get the value
var textareaElement = document.getElementById("userName");
function updateCount() {
document.getElementById("stringLength").innerHTML = textareaElement.value.length;
}
<label for="userName">User name:</label>
<textarea id="userName" onkeyup="updateCount()" cols="40" rows="3"></textarea><br>
<p id="stringLength">0</p>
When you have a reference to a DOM node (e.g, <textarea>), you can bind event handlers for the various events that it supports. In this case, we consult the HTMLTextAreaElement docs and learn that the following piece of JS would give the text length
const textarea = document.getElementById('userName');
const length = textarea.value.length; // textarea.textLength;
Then, we will consult the docs to determine that it is the input event that we want to bind to.
textarea.addEventListener('input', updateCount);
Your updateCount gets as its input the input event that also contains a reference to the event target, which is the textarea.
var textareaElement = document.getElementById("userName");
function textSize(event) {
document.getElementById("stringLength").innerHTML = event.target.value.length;
}
textareaElement.addEventListener("input", function(event){
document.getElementById("stringLength").innerHTML = event.target.value.length;
});
I know the post is old but I just had to figure this one out for myself so for anyone else that has trouble in the future here is the line you need.
textareaElement.addEventListener("blur", updateCount);
I´m trying to program html input onkeypress event from javascript but it doesn´t work, but I can program atributes like size or type.
var element3 = document.createElement("input");
element3.type = "text"
element3.size = "6"
element3.onkeypress= "return isNumberKeyDecimal(event)"
Is that possible?
The onkeypress property accepts a function, not a string.
element3.onkeypress = isNumberKeyDecimal;
But also take a look at the addEventListener function for the preferred approach to dealing with event listener functions.
In particular, you may wish to look at event delegation, which would allow you to have a single event listener on a container element rather than having to bind it to each input you create.
element3.setAttribute("onkeypress", "return isNumberKeyDecimal(event)");
element3.setAttribute("onkeypress", "return isNumberKeyDecimal(event)");
Mozilla reference here
It should be set as a function not a string.
element3.onkeypress = function(event){return isNumberKeyDecimal(event)}
I tried:
$('input').keyup(function() {
$(this).attr('val', '');
});
but it removes the entered text slightly after a letter is entered. Is there anyway to prevent the user from entering text completely without resorting to disabling the text field?
A non-Javascript alternative that can be easily overlooked: can you use the readonly attribute instead of the disabled attribute? It prevents editing the text in the input, but browsers style the input differently (less likely to "grey it out")
e.g. <input readonly type="text" ...>
if you don't want the field to look "disabled" or smth, just use this:
onkeydown="return false;"
it's basically the same that greengit and Derek said but a little shorter
$('input').keydown(function(e) {
e.preventDefault();
return false;
});
$('input').keypress(function(e) {
e.preventDefault();
});
If you want to prevent the user from adding anything, but provide them with the ability to erase characters:
<input value="CAN'T ADD TO THIS" maxlength="0" />
Setting the maxlength attribute of an input to "0" makes it so that the user is unable to add content, but still erase content as they wish.
But If you want it to be truly constant and unchangeable:
<input value="THIS IS READONLY" onkeydown="return false" />
Setting the onkeydown attribute to return false makes the input ignore user keypresses on it, thus preventing them from changing or affecting the value.
One other method that could be used depending on the need $('input').onfocus(function(){this.blur()}); I think this is how you would write it. I am not proficient in jquery.
For a css-only solution, try setting pointer-events: none on the input.
Markup
<asp:TextBox ID="txtDateOfBirth" runat="server" onkeydown="javascript:preventInput(event);" onpaste="return false;"
TabIndex="1">
Script
function preventInput(evnt) {
//Checked In IE9,Chrome,FireFox
if (evnt.which != 9) evnt.preventDefault();}
I like to add one that also works with dynamic javascript DOM creation like D3 where it is impossible to add:
//.attr(function(){if(condition){"readonly"]else{""}) //INCORRECT CODE !
to prevent actions on a HTML input DOM element add readonly to class:
var d = document.getElementById("div1");
d.className += " readonly";
OR in D3:
.classed("readonly", function(){
if(condition){return true}else{return false}
})
AND add to CSS or less:
.readonly {
pointer-events: none;
}
the nice thing about this solution is that you can dynamically turn it on and of in a function so it can be integrated in for example D3 at creation time (not possible with the single "readonly" attribute).
to remove the element from class:
document.getElementById("MyID").className =
document.getElementById("MyID").className.replace(/\breadonly\b/,'');
or use Jquery:
$( "div" ).removeClass( "readonly" )
or toggle the class:
$( "div" ).toggleClass( "readonly", addOrRemove );
Just to be complete, good luck =^)
just use onkeydown="return false" to the control tag like shown below, it will not accept values from user.
<asp:TextBox ID="txtDate" runat="server" AutoPostBack="True"
ontextchanged="txtDate_TextChanged" onkeydown="return false" >
</asp:TextBox>
One option is to bind a handler to the input event.
The advantage of this approach is that we don't prevent keyboard behaviors that the user expects (e.g. tab, page up/down, etc.).
Another advantage is that it also handles the case when the input value is changed by pasting text through the context menu.
This approach works best if you only care about keeping the input empty. If you want to maintain a specific value, you'll have to track that somewhere else (in a data attribute?) since it will not be available when the input event is received.
const inputEl = document.querySelector('input');
inputEl.addEventListener('input', (event) => {
event.target.value = '';
});
<input type="text" />
Tested in Safari 10, Firefox 49, Chrome 54, IE 11.
The best solution is to unfocus input once user clicks it so it makes it kinda readonly
onFocus={e => e.target.blur()}
I have a text input :
<input type="text" onkeydown="processText(this)" />
I have a processing function :
function processText(sender)
{
console.log(sender.value);
/// processing....
}
But then I check my value, its content hasn't been updated yet.
How can I do that?
Use onkeyup instead :
<input type="text" onkeyup="processText(this)" />
try onkeyup
http://www.w3schools.com/jsref/jsref_onkeyup.asp
Josh
If onkeyup is too late for your needs, you'll need to look at the key code of the event and then do something with the character that may or may not be added to the text box. Take a look at the DOM event reference or at the way jQuery handles it or the way Prototype handles it.