onchange Jquery not working when changing value with a selector - javascript

I'm trying to use sth like onchange but with no results.
i've been searching for a while but i didn't find.
HTML
<input type="text" id="myInput" />
<div id="button"></div>
JS
<script>
$("#button").on('click', function()
{
$("#myInput").val("new value");
});
$("#myInput").on('change keyup paste blur oncut propertychange', function()
{
//my code
});
</script>
I can't find a event where a JQ action is recognized like a changing in my input...
Thank you for your help!

Did you put these js code part into the $(document).ready(function(){
});
I think because these js is not loaded when the webpage is completed loaded.
$(document).ready(function(){
$("#button").on('click', function()
{
$("#myInput").val("new value");
});
$("#myInput").on('change keyup paste blur oncut propertychange', function()
{
console.log('event detected');
});
});
<html>
<title>Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<input type="text" id="myInput" />
<div id="button"></div>
</body>
</html>

Related

check changes to a <input type="text" > when value is changing from the back-end using JQuery

I have a text-field whose value will be set on the click of a button. I want to detect when the value of the input field will change. I can't figure out the specific event to fire.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function myFunction() {
$('#mytxtBox').val("2018/12/18");
}
$(document).on('input', '#mytxtBox', function() {
alert("Eureka!!!")
});
</script>
</head>
<body>
<input id='mytxtBox' type="text">
<button onclick="myFunction()">Press it</button>
</body>
</html>
Use trigger method to trigger the input event.
$('#mytxtBox').val("2018/12/18").trigger("input");
You need the change event. first you subscribe the input to the event listener, and add your logic that you want to fire when the event is called. Then you trigger the event after an update. Something like...
$(function(){//bind the event listener
$('#mytxtBox').on('change', function(){
//do stuff here
});
})
function myFunction() {
$('#mytxtBox').val("2018/12/18");
$('#mytxtBox').trigger('change');//trigger the event
}
Just assign the change a .change() and it will trigger the onChange event
$('#updateValue').on('click', function () {
$('#mytxtBox').val("2018/12/18").change();
});
$('#mytxtBox').on('change', function() {
console.log('abc');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='mytxtBox' type="text">
<button id="updateValue"> Press it </button>
Try this..
the alert will pop before the value appear
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function myFunction() {
$('#mytxtBox').val("2018/12/18").trigger("eureka");
}
$(function(){
$('#mytxtBox').on("eureka", function() {
alert("Eureka!!!")
});
});
</script>
</head>
<body>
<input id='mytxtBox' type="text">
<button onclick="myFunction()">Press it</button>
</body>
</html>
We can try using passing function as a callback to val() method.
Reference: http://api.jquery.com/val/#val-function
function myFunction() {
$('#mytxtBox').val(function(i ,value){
alert('hello i am going to change');
return "2018/12/18";
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='mytxtBox' type="text">
<button onclick="myFunction()">Press it</button>
There are multiple events for textbox such as keypress, change, keydown, keyup etc. You can select any as suitable for your goal.
Below example use keypress event:
function fnChangedText() {
alert("Eureka!!!");
}
function myFunction() {
$('#mytxtBox').val("2018/12/18");
$('#mytxtBox').keypress();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id='mytxtBox' type="text" onkeypress="fnChangedText()">
<button onclick="myFunction()"> Press it </button>

Make Text of One Div Same as Another

Anyone know how to copy the text of one div into another?
My situation is that I want to make a textbox that can have information typed into it and then show up in a div.
this is my code:
$("#title").text() = $("#t").text();
"#t" is my textbox and "#title" is the div.
The answer can be in javascript or jquery I don't mind. However I'd prefer jquery.
You can easily done it like this by handling 'onkeyup' event of the input text.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
</head>
<body>
<div>
<input type="text" id="txt1"/>
</div>
<div>
<p id="typed-result"></p>
</div>
<script>
$(document).ready(function(){
$('#txt1').on('keyup',function(){
var result = $(this).val();
$('#typed-result').text(result);
});
});
</script>
</body>
</html>
You could try the following:
$("#title").text($("#t").val());
$(function(){
$("#copyBtn").on("click", function(){
$("#first").text($("#txtBox").val());
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="first">This is the div.</div>
<input type="text" placeholder="enter value" id="txtBox"/>
<input type="button" value="Copy text box value to div" id="copyBtn"/>
You can do this with
$('#getDiv').text($(this).val());
Try with this working example , just type something in textarea
$(function(){
$('#getText').on('keyup', function() {
$('#getDiv').text($(this).val());
})
});
<textarea id="getText"></textarea>
<div id="getDiv"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
You can use the keyup JavaScript event, Something like this -
HTML -
<body>
<input type='text' id='one'>
<div id='two'></div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
</body>
jQuery -
$(document).ready(function() {
$('#one').on('keyup', function() {
$('#two').text($(this).val());
})
})
You can use this
$("#t").keyup(function(){
$("#title").text($("#t").value());
}
$('#txtArea').keyup(function(event) {
event.preventDefault();
$('.txt-block').text($("#txtArea").val());
// #txtArea is ur textarea box where you type text.
// .txt-block is your div where textarea Text shows.
});

Focus on input fires both 'focus' and 'select' event

I'm writing a Javascript script tightly bounded to events. I must execute some instructions on 'focus' event, and some different instructions on 'select' event.
With my surprise I see that giving focus programmatically, fires 'select' event too! That's a big problem.
Can you tell me why this is happening, and if I can block select event programmatically only when I give focus programmatically?
See the following example. When clicking to 'Give focus' button, console will write 'You selected something'!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<input id="field" type="text" />
Give focus
<script>
$(document).ready(function () {
$('#field').on('select', function(e) {
console.log('You selected something');
});
$('#btn').on('click', function(e) {
$('#field').focus();
})
});
</script>
</body>
</html>
Try this:
$(document).ready(function () {
$('#field').on('select', function(e) {
console.log('You selected something');
});
$('#btn').click( function(e) {
$('#field').focus();
})
});
<input id="field" type="text" />
Give focus
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

JQuery trigger click event not working

I'm having trouble getting the following to work:
$('#elem').trigger('click');
When I run it, it doesn't trigger a click but it doesn't show any error logs in the console either.
I've also tried the following with no luck:
$('#elem')[0].click();
Any ideas as to what could be going wrong or a solution for this?
Update: Jquery is working and when I inspect the element, #elem does appear (it's an id for a button)
HTML:
<a:button id="elem">My Button</Button>
JQuery:
P.when('A', 'jQuery').execute(function (A, $) {
//when this function is called, it should trigger a button click
triggerButtonClick = function() {
$('#elem').trigger('click');
};
});
Try it in document.ready function, then all dom loads when the document is ready.You can do something like this based on your requirement.
$( document ).ready(function() {
$('#radio').click(function(){
$('#elem')[0].click();
})
});
i have tried this code and it works
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("chk1").click(function(){
$('#usr').trigger('click');
});
});
</script>
</head>
<body>
<form action="sample.php">
<button id="chk1">click</button>
<input class="form-control" type="submit" id="usr">
</form>
</body>
</html>
this code works for me:
$(window).load(function() {
$('#menu_toggle').trigger('click');
});
$(document).ready(function () {
$('#elem').click(function () {
alert('clicked');
$(this).css('color', 'red');
});
$('#elem').trigger('click');
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<body>
<div id="elem" >test</div>
</body>
</html>

jQuery .focusout() also when .keyup() occurs in Firefox 33.1

when I release a key in an input form html field, I get in Firefox 33.1 in jQuery an keyup event and also an unwanted focusout event. I just want the keyup event when releasing the key.
This is my code:
<html>
<head>
<script src="libraries/jquery/jquery-1.11.1.min.js"></script>
</head>
<body>
<form id="contact_form">
<input class="contact_save" type="text" value="" />
</form>
</body>
<script>
$(document).ready(function($){
$('#contact_form .contact_save').focusout(function() {
alert("focusout!");
});
$('#contact_form .contact_save').keyup(function() {
alert("keyup!");
});
});
</script>
</html>
What I am doing wrong?
Thanks
Michael
It's because the alert causes the focusout to be called. Putting the result in another element for an example does not cause it to trigger:
$('#contact_form .contact_save').focusout(function() {
alert("focusout!");
});
$('#contact_form .contact_save').keyup(function() {
$('#result').val('keyup');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="contact_form">
<input class="contact_save" type="text" value="" />
<output id="result" />
</form>
Trying using:
$('#contact_form .contact_save').blur(function() {
alert("focusout!");
});

Categories

Resources