I’ve been reading but can’t seem to get a form field to be updated after a JQuery JSON request:
<script>
$(document).ready(function(){
$("#client").live('change',function(){
$.getJSON('client_get_json.php?id=' + $(this).val() , function(data) {
//$("input[type=text][name=contact_mob]").html(data.contact_mob); <- no good
//$("input[id='contact_mob']").html(data.contact_mob); <- no good
$("#testdiv").html(data.contact_mob);
});
});
});
</script>
#testdiv upadates fine with the contact_mob data but I can’t get the right syntax for updating the field name=”contact_mob” id=”contact_mob” which I want to repopulate with all the other contact fileds if the user happens to change the contact.
Also any advice on the correct way to do this kind of thing would be welcomed.
All the fileds are populated from PHP when the document first loads.
You should use the
val()method to set the value of a input field. If the element has a id you can simply use the id selector#elementIdto select it. Try this.If the element do not have an id but it has a name then you can use attribute(name) selector like this.
You can do the same for all the form fields and populate them.