I'm a noob with JQuery so with that said, what I need to do is get an id that's in a hidden input. This id will dynamically change and I need JQuery to be made aware of this change and alert the id that's in the input every time it changes. What would I use to do this?
1 Answer
If the value of the hidden input is changed by code, there is no native DOM event which will be raised for you to capture.
Instead you would have to manually raise an event, or just fire off a function at the point in code where the value changes. Something like this:
$('#foo').val(1337).trigger('dynamicChange');
// in another file, far far away
$('#foo').on('dynamicChange', function() {
// do stuff
});
10 Comments
NaN
Rory, thank you for the code sample. I'll give this a shot now and see if it works.
NaN
Rory, I'm sort of at a loss with your code. It's not making too much sense to me. Is dynamicChange internal to JQuery or a function that I would have to write?
Rory McCrossan
@user1709311
dynamicChange is just the label given to the event I'm raising - you could call it whatever you like. So long as the name of the event matches in the on() parameter, it'll work. Here's more info on trigger in the API.NaN
Rory, thanks. I'm really new to JQ so thanks again for sticking with me. I'll have a look now.
NaN
Rory, let me ask you a question. Where I am using innerHTML to change the value dynamically, this file was written in pure JavaScript. The first code snippet you have there is JQuery and it doesn't seem to work when I include it in the JavaScript file. I understand the concept now of what's happening but can't get it working. Any ideas?
|
id. The alternative (DOM mutation events) is not portable enough (yet).googledoesn't hurt.