Udders
Udders

Reputation: 6986

programmatically change element value javascript

I wanting to get the currentTarget to pass to a function, once I have changed the value of a select programmatically.

Is this possible? Something like,

$('input[name="client_id"]').val(model.get('id')) //Change the value of select
this.doSave( $('input[name="client_id"]')); //Do save expects event(e) as parameter

function doSave(event){
    console.log($(event.currentTarget);
}

I have simplified this down massively, basically I am changing a select programmatically, and then want to fire my save event, which normally gets triggered on a change event, and as a result takes an event as a parameter, but all I have is the DOM element? Is there a way to send it an event?

Upvotes: 0

Views: 145

Answers (2)

U.P
U.P

Reputation: 7442

First of all, This

$('input[name="client_id"]')

apparently, changes the value of an input and not select.

This

$('select[name="client_id"]').val(model.get('id'))

changes the selected option in a select.

You can bind the change event on a select like

$('select').change(function(event){
    //use event here 
    //or pass to doSave(event)
});

and it will be fired when call

$('input[name="client_id"]').change();

Upvotes: 0

VishwaKumar
VishwaKumar

Reputation: 3433

Use the change event handler as below :

$('input[name="client_id"]').on('change', function(event) {
   console.log($(event.currentTarget);
});

or

$('input[name="client_id"]').on('change', doSave);

function doSave(event){
    console.log($(event.currentTarget);
}

Upvotes: 1

Related Questions