1

For some reason, when I enter a value in the following script, the value of ISBN is always undefined. I'm not sure why this is the case. I have set the button to run the function onclick as well...

HTML:

<form id="frm1">
    Book ISBN: <input type="text" name="fname"><br><br>
    <input type="button" onclick="myFunction()" value=行きましょう!>
</form>

JavaScript

function myFunction() {
    var isbn = document.getElementById("frm1").submit(); // Replace with ISBN / EISBN / ASIN of the book
    alert("Starting!");
    alert(isbn);

    var api_key = "cf6f5fe91531a855495d77fc06dd9ada6f1fa1f7"; //Replace with the developer key (Submit the form to get a key)

    //Optional Settings
    var smiley = 'true'; // Change to false if do not want to show smiley with each review
    var width = 700;     // Width of the reviews iframe
    var height = 400;    // Height of the reviews iframe
    var wf = document.createElement('script');
    var host = "idreambooks.com";  

    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
              '://' + host + '/reviews_widget.js?api_key=' + api_key + '&isbn=' + isbn +
              '&smiley=' + smiley + '&width=' + width + '&height=' + height;
    wf.type = 'text/javascript';
    wf.async = 'true';

    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
}

Is there anything I'm missing about how to get input values?

1
  • 1
    submit() method doesn't have return value, that's why you're getting undefined Commented Feb 1, 2017 at 20:26

2 Answers 2

6
var isbn = document.getElementById('someIdYouAssignTheInput').value;

As mentioned above, submit doesn't return anything, which is why the value is undefined. You want to get the value of the input element. The best way to do that is to assign it an id, then use the code I included above.

Good luck!

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

Comments

5

try

 var isbn = document.getElementsByName("fname")[0].value;

to retrieve the input text box's value.

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.