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?
Asked
Active
Viewed 103 times
1
-
1You would typically call a function every time you change the `id`. The alternative (DOM mutation events) is not portable enough (yet). – Frédéric Hamidi Aug 22 '13 at 08:25
-
2Have you tried anything at all? And how are you changing the value? – putvande Aug 22 '13 at 08:25
-
http://stackoverflow.com/questions/6533087/jquery-detect-value-change-on-hidden-input-field – kayen Aug 22 '13 at 08:25
-
1Btw, a little bit of `google` doesn't hurt. – kayen Aug 22 '13 at 08:26
-
@putvande. I've been trying and reading for hours. I have nothing that is worthy of being looked at. innerHTML is what I'm using on the other side to change this value – NaN Aug 22 '13 at 08:27
-
So you know when it is changing? Why don't you call a function / do what you need to do in the same function? – putvande Aug 22 '13 at 08:28
-
@kayen. How do you know that I've not been googling for the answer? Please don't assume. – NaN Aug 22 '13 at 08:29
-
@putvande. I do not know when it's changing. innerHTML dynamically changes the value with each iteration of the list – NaN Aug 22 '13 at 08:31
1 Answers
4
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
});

Rory McCrossan
- 331,213
- 40
- 305
- 339
-
Rory, thank you for the code sample. I'll give this a shot now and see if it works. – NaN Aug 22 '13 at 08:28
-
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? – NaN Aug 22 '13 at 08:36
-
@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](http://api.jquery.com/trigger/). – Rory McCrossan Aug 22 '13 at 08:44
-
Rory, thanks. I'm really new to JQ so thanks again for sticking with me. I'll have a look now. – NaN Aug 22 '13 at 08:49
-
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? – NaN Aug 22 '13 at 09:15
-
-
This is kind of complex for me to even explain because I'm so new to this. I basically have a single JS file that has a document ready function in it. Within it, I have all of my JQ functions that I need. Then I have another file that is written in pure JavaScript. I have a bootstrap file that loads, JQuery, my JQ function file and then this JS file that has all of the functions that operate the player I'm working with. I just don't know how to integrate the functions into this file. – NaN Aug 22 '13 at 09:20
-
If I pull this function `function setMediaId(v) { $('#mediaid').val(v).trigger('change'); }` out of the ready function, I can get an alert. If I put it in the ready function, nothing happens. – NaN Aug 22 '13 at 09:21
-
It sounds like you're probably best to start another question about that. If you include details about what is in each script I'm sure someone will be able to help you. – Rory McCrossan Aug 22 '13 at 09:33
-
You and I are on the same page because I was thinking the same thing. I'll start that thread now and I'll include what I've got so far. Thanks, Rory! – NaN Aug 22 '13 at 09:34