Alex Knopp
Alex Knopp

Reputation: 935

How to update input value attribute on the fly

Thanks in advance for the assistance.

I have some fields in a form that send some data to an ajax request. I have a button that fires this function to get the data to fill in the remaining form fields on the page.

Save adding all my code, as there is nothing wrong with it at the moment, here is an example of what I am trying to do.

<input type="text" value="corporate" id="type" placeholder="New Value">
<button id="update"></button>

The update button is used to make an ajax request to the database to get all the remaining meta data for that page. Which kind of works. However the id there "type" is the type of data i need (commercial / corporate / retail). Each type has a different set of meta data.

I need to replace the value attr when i click the button before the remaining functions for the ajax request fire off so that they take the correct data with them. So if i type a new value, it sends the new value.

make sense?

Upvotes: 1

Views: 462

Answers (2)

Ray Lionfang
Ray Lionfang

Reputation: 687

Use a select field instead. This is what select fields are for.

Upvotes: 1

user4227915
user4227915

Reputation:

Do you mean something like this?

$(".update").on("click", function(){
    var new_val = $(this).text();
    $("#type").val(new_val);
});

Upvotes: 1

Related Questions