0

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); 
 });
 });
});
asked Dec 19, 2019 at 12:37
1
  • Just move $("#description").val(data.description); into separate function and pass data.description as param. Commented Dec 19, 2019 at 12:39

2 Answers 2

1
$("#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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.