1

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?

8
  • 1
    You would typically call a function every time you change the id. The alternative (DOM mutation events) is not portable enough (yet). Commented Aug 22, 2013 at 8:25
  • 2
    Have you tried anything at all? And how are you changing the value? Commented Aug 22, 2013 at 8:25
  • stackoverflow.com/questions/6533087/… Commented Aug 22, 2013 at 8:25
  • 1
    Btw, a little bit of google doesn't hurt. Commented Aug 22, 2013 at 8: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 Commented Aug 22, 2013 at 8:27

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

10 Comments

Rory, thank you for the code sample. I'll give this a shot now and see if it works.
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?
@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.
Rory, thanks. I'm really new to JQ so thanks again for sticking with me. I'll have a look now.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.