0

I need help with the following code please:

http://jsfiddle.net/geetyn6f/422/

What I need to do is to write the URL in the input

And then when I press 'Check' the answer will appear in the second input instead of the alert.

URL: <input type="text" name="name">
<button type="submit" value="Submit">Check</button>
Answer: <input type="text" name="answer">


$.ajax({
    url:'http://www.example.com/3.zip',
    error: function()
    {
       alert('file does not exists');
    },
    success: function()
    {
        alert('file exists');
    }
});

// Now to check if file exists call function
checkIfRemoteFileExists('http://www.yoursite.com/abc.html');

checkIfRemoteFileExists('http://www.google.de');
John
  • 1,313
  • 9
  • 21
lily
  • 1
  • 6

1 Answers1

0

You have some missing pieces in your code. But, the overall idea is to get the text of the URL box every time you want to make the request. After making the request, update the answer box. This is only a way to get those values. JQuery has a simpler way to do it. I'll leave that research to you. Click on Run Code Snippet and try it.

function checkIfRemoteFileExists(){

  var answerBox = document.getElementById('answer'); //get the box

  $.ajax({
    url: document.getElementById('name').value, //get the value from the url box
    error: function()
    {
       answerBox.value = "file does not exists"; //update answer box
    },
    success: function()
    {
       answerBox.value = "file exists"; //update answer box
    }
  });
}

//attach function to button
document.getElementById("button").addEventListener("click", checkIfRemoteFileExists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
URL: <input type="text" id="name">
<button type="submit" id="button" value="Submit">Check</button>
Answer: <input type="text" id="answer">
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69