I trying to make a list in which it has a description field box in which I am getting a description Output...
This is my Javascript code In which I need to split the description function.
How to split the function...
<script type="text/javascript">
$(document).ready(function() {
$("#selectedid").change(function() {
var selectedid = $(this).val();
$.getJSON("http://localhost:8080/todos/"+selectedid, function(data){
$("#description").val(data.description);
});
});
});
2 Answers 2
$("#selectedid").change(function() {
var url = 'http://localhost:8080/todos/' + $(this).val();
$.getJSON(url, setDescription);
});
function setDescription(data) {
$("#description").val(data.description);
}
answered Dec 19, 2019 at 12:51
BadHorsie
14.6k31 gold badges127 silver badges196 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can split the functions as you want like the following callback functions.
$(document).ready(function() {
$("#selectedid").change(function() {
var selectedid = $(this).val();
var url = "http://localhost:8080/todos/" + selectedid;
sendGet(url, handleResponse);
}
function sendGet(url, callback) {
$.getJSON(url, callback);
}
function handleResponse(data) {
$("#description").val(data.description);
}
});
$(document).ready(function() {
$("#selectedid").change(function() {
var selectedid = $(this).val();
var url = "http://localhost:8080/todos/" + selectedid;
$.getJSON(url, handleResponse);
}
function handleResponse(data) {
$("#description").val(data.description);
}
});
answered Dec 19, 2019 at 12:42
Allen Haley
6576 silver badges21 bronze badges
Comments
lang-js
$("#description").val(data.description);into separate function and pass data.description as param.