Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: open

Message Too Old, No Replies

jQwery on onchange

   
3:42 pm on Feb 6, 2012 (gmt 0)

5+ Year Member



I have a `input` text field which is filled with the url of an image using a jQuery function.

After changing the url, I need the new image displayed. But when I use the `.change` jQuery event, the image only gets updated if the field is changed by the keyboard.

I have the function that updates the image but how do I trigger it even when the field is changed via another function rather than the keyboard?

How do I use the "onChange" event of the `input` to run the jQuery function?

This is the function which sets the value of the input box with a url

function selectURL(url) {
if (url == '') return false;
field = window.top.opener.browserWin.document.forms[0].elements[window.top.opener.browserField];
field.value = url;
if (field.onchange != null) field.onchange();
}

The function which updates the image:

$(document).ready(function() {
$('#image').change(function() {
var src = $(this).val();
$('#imagePreview').html(src ? '<img src="' + src + '">' : '');
});
});

Probably if using the `onchange` element of the input box for executing the jQuery function will be working properly and I was asking for this!
6:29 am on Feb 7, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Try changing
if (field.onchange != null) field.onchange();
to
$(field).change();