0

I am new to JavaScript and am looking for some help. I am trying to make a JSON request based on textbox input. I want the text form the box to be a part of the request, however I cannot make the text be a string so that it can be used for the JSON request. What am I doing wrong?

Here is my code:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>


<p id="syno"></p>


<form name="test">
<input type="text"  name="Edit"> <!-- where i input a word-->
<input type="button" value="Test" onClick="gettext()"><!-- test button-->
<input type="text" name="Edit2" value="power" readonly style="border:1px solid white">
</form>


<script>
function gettext() {
document.test.Edit2.value = document.test.Edit.value;
document.test.Edit2("Edit2").value = document.test.Edit.value;
}


var apiKey = '0771a4c95ca7e23d41e33f67a1da0000/';
var txtjson = '/json';
var f = document.test.Edit2.value;
var apiUrl = 'http://words.bighugelabs.com/api/2/' + apiKey + f + txtjson;

$.ajax({
        url: apiUrl,
        type: "GET",
        dataType: 'json',
        success: parseWord
    });

function parseWord(data) {
           $('#syno').text(data.noun.syn);
         }

</script>
</body>
</html>

1 Answer 1

1

issues in code

  1. you have added ajax code but code is outside of gettext() function
  2. code line document.test.Edit2("Edit2") should be as document.forms.test.Edit2.value

see correct code,

function gettext() {

  document.forms.test.Edit2.value = document.forms.test.Edit.value;
  var apiKey = '0771a4c95ca7e23d41e33f67a1da0000/';
  var txtjson = '/json';
  var f = document.forms.test.Edit2.value;
  var apiUrl = 'http://words.bighugelabs.com/api/2/' + apiKey + f + txtjson;

  $.ajax({
    url: apiUrl,
    type: "GET",
    dataType: 'json',
    success: parseWord
  });
  return false;
}




function parseWord(data) {
  $('#syno').text(data.noun.syn);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="syno"></p>
<form name="test">
  <input type="text" name="Edit" />
  <!-- where i input a word-->
  <input type="button" value="Test" onClick="return gettext(this);" />
  <!-- test button-->
  <input type="text" name="Edit2" value="power" readonly style="border:1px solid white" />
</form>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.