yong jie
yong jie

Reputation: 73

Unable to change a value inside a variable?

I am currently working on a project that I took over with the input reads from a barcode, currently iam having trouble with my output giving me a fix value which the code reads from and does not read from the actual input.

below is the html code for the scanner. it reads fine

<div class="section" id="instruction-3">
            <p>Otherwise, scan your code at the bottom, or manually type it in here:</p>
            <span>
                <input type="text" id="IC-input" name="IC" onclick="openKeyboard()" onkeyup="autofill(this.value)" placeholder="Enter your IC Number" required maxlength="9">
                <label><button type="button" id="theButton" onclick="theButtonIsPressed()">Submit</button></label>
            </span>
        </div>

follow by the javascript (which i suspect is where the problem lies but am not sure)

<script>
    var NRIC = '{"NRIC":"0"}'; 

    function theButtonIsPressed(){
        closeKeyboard();
        console.log('button clicked');
        NRIC = '{"NRIC":"123456789"}';
        //var IcNum = document.getElementById("IC-input").value;
        //NRIC = NRIC.replace("0", IcNum);
        document.getElementById("IC-input").value = "";
        doWork(NRIC)
    }
</script>   

        function doWork(NRIC) {
        // ajax the JSON to the server
        $.post("receiver", NRIC, function(){
        });
        // stop link reloading the page

function update() {
        setInterval(function(){$.post("receiver", '{"NRIC":NRIC}', function(){});}, 900);

It keeps giving me the value inside NRIC = '{"NRIC":"123456789"}'; which is 123456789, i realize this might be a simple fix but i am still learning and am unsure.

thank you in advance.

Upvotes: 1

Views: 78

Answers (3)

yong jie
yong jie

Reputation: 73

all but I have solved the problem, it was my fault that I did not mention I wanted to transfer the data to another webpage, and the problem with that was that the data was not transferred over to the other html.

function autofill(value){
        console.log("Autofill:"+value)
        //console.log(9 digits);
        button = document.getElementById("theButton");
        if(value.length == 9){
            console.log('form is ready to submit');
            theButtonIsPressed(value);
        } 
    }


    function theButtonIsPressed(value){ 
        closeKeyboard();
        console.log('button clicked');
        //NRIC = '{"NRIC":"123456789"}';
        console.log(value)
        doWork(value)
    }

by doing this it read the value as accordingly from the input and worked after, sorry again for my confusing question and thanks to everyone who tried to help.

Upvotes: 0

Andrea Viviani
Andrea Viviani

Reputation: 267

If I correctly understanded you want to have in the obj the input value, so try this:

function theButtonIsPressed(){
    closeKeyboard();
    console.log('button clicked');
    var IcNum = document.getElementById("IC-input").value;
    NRIC.NRIC = IcNum;
    doWork(NRIC)
}

Upvotes: 1

benhatsor
benhatsor

Reputation: 2033

Why quotes around an object?

var NRIC = {"NRIC": 0}; 
function theButtonIsPressed() {
  NRIC = {"NRIC": 123456789};
  doWork(NRIC)
}

Upvotes: 0

Related Questions