1

I have read some solutions to this, none of which are hassle free, so I thought maybe some modern solution now exist to remedy this. It should anyways, since the problem has been around for a while. Something in the more recent jQuery updates perhaaps?

I set my form field like this:

parent.window.document.getElementById(parent.window.imageInputField).value = '{{ path }}'+image;

and I have an onchange event like this that I would like to fire upon my field update as of above.

$('#data_1').change(function(){
img = "url(" + $('#data_1').val() + ")"
$('#slide_bg').css("backgroundImage", img );
});

UPDATE

so this code works fine:

alert( $('#'+parent.window.imageInputField, window.parent.document).val() );

while this doesn't do anything, no error or nothing

$('#'+parent.window.imageInputField, window.parent.document).change();

The change event is defined here

$('#data_1').change(function(){
alert('change event called');
img = "url(" + $('#data_1').val() + ")"
$('#slide_bg').css("backgroundImage", img );
});

and this works perfect:

<input type='button' value='Uppdatera förhandsgr.' onclick="$('#data_1').change()" />

All I want is to trigger what this button does, but by code... anyone see where I went wrong?

Matt Welander
  • 8,234
  • 24
  • 88
  • 138
  • I don't understand, you are looking for an onChangeProperty event? – HMarioD Dec 29 '12 at 19:43
  • Yes, that fires when I javascript-programattically change the contents of an input field (the onchange event only fires when the user puts the cursor in the field, updates the contents and blurs from the field. – Matt Welander Dec 29 '12 at 20:36

1 Answers1

7

you need to trigger the change handler with your code. Updating a value with script will not trigger change event

parent.window.document.getElementById(parent.window.imageInputField).value = '{{ path }}'+image;
$('#data_1').change()
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Oh, I see. Is there a good way to call that from withing this IFRAME (as is obvious from parent.window.document etc. the form field populated, thus it's change event, is within the parent window... I tried both window.opener.$("#data_1").change() and $( "#data_1", window.opener.document ) without success... – Matt Welander Dec 29 '12 at 20:54
  • is jQuery available in iframe? – charlietfl Dec 29 '12 at 21:04
  • 1
    try `$(parent.window.document).find('#data_1').change()` – charlietfl Dec 30 '12 at 20:22
  • not working =( I am sure I have stumbled across a way of doing this like a year ago, but now I can't seem to find it =( – Matt Welander Dec 31 '12 at 18:02
  • I see now however that I can access the element because I can print out that form fields value, it's obviously just a matter of fireing the change function, that's where my prob is... – Matt Welander Dec 31 '12 at 18:12
  • I accepted your answer, I see the principle works in example code, though I can't get it to work in my implementation, the error must be somewhere else, thanks for solving that step for me though! – Matt Welander Jan 11 '13 at 15:58