triggering onchange event - javascript

I'm not able to get the below code to work.
$("#grid_table td:nth-child(10) input").live("onchange",function () {
alert("changed");
});
am I missing something here? Thanks.
Ravi

Try changing onchange to change..
$("#grid_table td:nth-child(10) input").live("change",function () {
alert("changed");
});
If that doesn't work I would verify your selector is working correctly.

Have you tried this:
$("#grid_table td:nth-child(10) input").change(function () {
alert("changed");
});
to make sure your selector is working properly?

Related

How to trigger Bootstrap fileinput change event?

I am using fileinput control managed by this js in my project. I want to validate if fileinput has file or not. For that, I am trying to trigger change event of fileinput control. But change event is not being triggered. This is my fiddle. Below is my change event to catch changes done to Fileinput.
$(document).ready(function() {
$(document).on('change', '#testImageInput', function () {
alert($(this).val());
});
});
Thank you for suggestions and answers.
here is the updated fiddle link check it
https://jsfiddle.net/b7059pk1/2/
$('#testImageInput').change(function () {
alert($(this).val())
});
It depends on the jQuery version, try this:
<script type="text/javascript">
$(function() {
$("#testImageInput").change(function (){
alert($(this).val());
});
});
</script>
It is always better to target element directly instead of whole document.
Updated fiddle --
https://jsfiddle.net/b7059pk1/1/
$(document).ready(function() {
$("#testImageInput").on('change', function () {
alert($(this).val());
});
});
P.S : I have used jquery 3.2.1 in the updated fiddle

Working with dynamic jQuery

I was wondering how to make this trigger work as I've tried all I can, but can't seem to find out the problem.
HTML
<div id="testFunc">Change AHref Class</div>
test
JavaScript
$(function () {
$("#testFunc").click(function () {
$('#testLink').removeClass("button");
$('#testLink').addClass("button2");
return false;
});
});
$(function () {
$(".button2").click(function () {
alert('test');
return false;
});
});
Somehow, the upon triggering testFunc which changes the source dynamically which causes the a href class to change from button to button2, it doesn't seem to register when i use button2 click.
Is there anyway to solve this?
Thanks!
Try .on()
Use Event Delegation.
$(document).on('click','.button2',function(){ code here });
Syntax
$( elements ).on( events, selector, data, handler );
Demo Working Fiddle , Problem Fiddle

Select all elements except ... in jQuery

I am working on a project, what includes a smartsearch engine.
I'd like to write a method what makes the client write inside the smartsearch, even when it's not focused. F.e. browsing the site, the client hits down a key, and the focus jumps to the smartsearch.
That's working fine with this simple code:
$(document).ready(function()
{
$("*").keydown( function()
{
$("input.ss-24#b").focus();
});
});
But, yeah as You can see, it unfocuses other inputs too, and that's not the way I want it.
I have tried several 'possible-solutions', like :not() and even .not() method like:
$(document).ready(function()
{
$("*").not("input").keydown( function()
{
$("input.ss-24#b").focus();
});
});
But it still unfocuses fields with "input" tagname too. What should I do to force jQuery not to select input fields for this event listener?
Thanks, Steven.
$(document).keydown(function() {
if (!$('input').filter(':focus').length) {
$('#b').focus();
}
});
Working fiddle
$(document).ready(function () {
$(document).keydown(function (e) {
$("input.ss-24#b").focus();
});
});
Does this work?
http://jsfiddle.net/Xbbu8/

jQuery preventDefault function not working on jsfiddle

I cannot get the following simple jQuery to work. I know I am overlooking something, so please help me out. Here is the code at http://jsfiddle.net/Z5waA/. It's a simple click the button, then alert with jQuery.
preventDefault(e); should be e.preventDefault();
code should be like this,
$('#submitResetPass').bind('click', function(e) {
e.preventDefault();
alert("hello");
});
and you need to add jQuery reference.
You had a couple issues. First, the jsFiddle wasn't set for jQuery. Then, your call to preventDefault wasn't correct. It works here: http://jsfiddle.net/jfriend00/v9aVb/.
$('#submitResetPass').bind('click', function(e) {
e.preventDefault();
alert("hello");
});
Use e.preventDefault() instead of preventDefault(e).

JQuery drop down bobs up and down continuously

I am using jquery to hide/show an DIV on hover of an LI. When I do this the div appeared but pops up and down without stopping until I take my mouse off the LI.
$(document).ready(function () {
$('li.menu_head').mouseover(function () {
$('div.newsadviceDrop').slideToggle('medium');
});
});
Try:
$(document).ready(function () {
$('li.menu_head').hover(function () {
$('div.newsadviceDrop').slideToggle('medium');
});
});
EDIT: To keep it open until you mouse over again do this
$(document).ready(function () {
$('li.menu_head').mouseenter(function () {
$('div.newsadviceDrop').slideToggle('medium');
});
});
Try to use .mouseenter instead of .mouseover
While the mouse is over li.menu_head, you will repeatedly call .slideToggle, which explains why you see the continuous behaviour. You could perhaps replace this with behaviour on mouseenter and mouseexit.
There is an issue like that with "mouseover".
Try "mouseenter" instead...
OR...
just use the jQuery hover()
EDIT:
silent1mezzo submitted the best/correct answer first.

Categories

Resources