7

I tried to collect data from JSON to fill out related Input Text Field after matching results, but it's not working.

How can I get populate the input text field from the JSON?

JS Code

$(document).ready(function(){
var filter = document.getElementById('zipcode');
var JSONtbl = [
		{"zipcode":01702,"address":"334 CONCORD ST","County":"MIDDLESEX"},
		{"zipcode":02482,"address":"27 Atwood St","County":"NORFOLK"},
		{"zipcode":02459,"address":"189 Cypress St","County":"MIDDLESEX"}
	     ];
filter.onkeyup = function() {
    var zipcodeToSearch = filter.value;
    var n = zipcodeToSearch.length;
    if (n < 5) {
    	document.getElementById("address").innerHTML = "";
    	document.getElementById("County").innerHTML = "";
    } else {
        for (var i = 0; i < JSONtbl.length; i++) {
        	if (JSONtbl[i].zipcode == zipcodeToSearch) {
        		document.getElementById("address").innerHTML = JSONtbl[i].address;
        		document.getElementById("County").innerHTML = JSONtbl[i].County;
             }
        }
        if (document.getElementById("address").innerHTML == "") {
            alert("ZipCode Out Of Area")
        }
    }
};
});
div {
    padding: 2px 5px;
}
<form method="post">
<div><input type="text" id="zipcode"/></div>
<div><input type="text" id="address" disabled="disabled"></div>
<div><input type="text" id="County" disabled="disabled"></div>
</form>

3
  • 4
    Use value instead of innerHTML Commented Apr 20, 2016 at 18:33
  • thanks for helping Commented Apr 20, 2016 at 19:28
  • If you found the answer helpful, please upvote it. Commented Apr 21, 2016 at 2:32

4 Answers 4

11

Two mistakes in your code.

First: Input does't have innerHTML but value. Second You are assigning integer to zipcode starting with zero. Rather you need a string type because value returned by input will be a string.

use this code

var filter = document.getElementById('zipcode');
var JSONtbl = [
		{"zipcode":"01702","address":"334 CONCORD ST","County":"MIDDLESEX"},
		{"zipcode":"02482","address":"27 Atwood St","County":"NORFOLK"},
		{"zipcode":"02459","address":"189 Cypress St","County":"MIDDLESEX"}
	     ];
filter.onkeyup = function() {
    var zipcodeToSearch = filter.value;
    var n = zipcodeToSearch.length;
    if (n < 5) {
    	document.getElementById("address").value = "";
    	document.getElementById("County").value = "";
    } else {
        for (var i = 0; i < JSONtbl.length; i++) {
            
        	if (JSONtbl[i].zipcode == zipcodeToSearch) {
           
        		document.getElementById("address").value = JSONtbl[i].address;
        		document.getElementById("County").value = JSONtbl[i].County;
             }
        }
        if (document.getElementById("address").value == "") {
            alert("ZipCode Out Of Area")
        }
    }
};
div {
    padding: 2px 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="post">
<div><input type="text" id="zipcode"/></div>
<div><input type="text" id="address" disabled="disabled"></div>
<div><input type="text" id="County" disabled="disabled"></div>
</form>

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

Comments

0

Input elements do not have a inner html property. Rather they have a value property. So this:

document.getElementById("address").innerHTML = JSONtbl[i].address;
document.getElementById("County").innerHTML = JSONtbl[i].County;

should be :

document.getElementById("address").value = JSONtbl[i].address;
document.getElementById("County").value = JSONtbl[i].County;

Comments

0

try this!

$(document).ready(function(){
        var filter = document.getElementById('zipcode');
        var JSONtbl = [
                {"zipcode":"01702","address":"334 CONCORD ST","County":"MIDDLESEX"},
                {"zipcode":"02482","address":"27 Atwood St","County":"NORFOLK"},
                {"zipcode":"02459","address":"189 Cypress St","County":"MIDDLESEX"}
                 ];
        filter.onkeyup = function() {
            var zipcodeToSearch = filter.value;
            var n = zipcodeToSearch.length;
            if (n < 5) {
                document.getElementById("address").value = "";
                document.getElementById("County").value = "";
            } else {

                for (var i = 0; i < JSONtbl.length; i++) {

                    if (JSONtbl[i].zipcode == zipcodeToSearch) {

                        document.getElementById("address").value = JSONtbl[i].address;
                        document.getElementById("County").value = JSONtbl[i].County;
                     }
                }
                if (document.getElementById("address").value == "") {
                    alert("ZipCode Out Of Area")
                }
            }
        };
    });

1 Comment

Welcome to Stack Overflow! Please add some explanation of why this code helps the OP. This will help provide an answer future viewers can learn from. See How to Answer for more information.
-1

I think that you can do like this:

$('#Input_id').val(JsonValue);

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.