User
User

Reputation: 603

Javascript - Event once an element has a value

I have a hidden input, and during the logic I set the value of that hidden input.

Is there a event once this hidden input value is set ?

Given the hidden input added on the fly

Upvotes: 1

Views: 514

Answers (2)

Aalexander
Aalexander

Reputation: 5004

Problem

Changes the value to hidden elements don't automatically fire the change event.

Solution

So you have to tell it jquery to fire it and you can do this with the .change() method.

let input = $("#example");
input.hide();
input.change(function(){
  console.log("Value has changed");
});




input.val(10).change();
input.val(30).change();


$("form").append(`<input type="text" value="${input.val()}">`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="example" placeholder="Type in a value" name="name"/>
<p id="values"></p>


<form>

</form>

Upvotes: 0

A Paul
A Paul

Reputation: 8261

when you set the value of the hidden input can you trigger an change event and then catch that? something like below. As you said "I set the value" I assume you could do that.

$('#yourElelentId').change(function(){
 
});

$('#yourElelentId').val(100).trigger('change');

Upvotes: 1

Related Questions