1

My problem is: I have two input boxes one for a code and one for a price:

<input class="style" name="code" id="code" type="text">
<input class="style" name="price" id= "price" type="text" value="&euro; 15,00" readonly="readonly">

Now i want to change the price by code (like a check from a code array [code1][code2]. result to code1 = price/2 and code2 = price/4 ) This i want to check by a script in real time or befor the post (on submit). This this possible when yes how? Or is there another better way to do this ?

I have:

while(true) 
{
    var code = document.getElementById("code").value;

    var codes = new Array("promo1", "promo2");

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

        if (codes[i] == code) {
            document.getElementById("price").value = "5";
        }       
    }
}

and my input fields are obviously in a form-field but the value doesnt change, but why ? I dont have to change in real time it would be ok if the post sends the new value...any solutions/better ideas/hints ? I know there is JQuery a better approach but it shouldn't be a problem with js.

fiddle link:
jsfiddle.net/vicR/4Mtbr

2 Answers 2

2

This can be obtained easily in many ways, specially if one takes use of libraries or frameworks, but with pure javascript it should be done as so:

while(true) {
 var code = document.getElementById("code").value
 //now check for valid code in some sort of map or array
 //REMEMBER javascript supports string indexed arrays such as arr["#45"]
 if(code == validatorFunctionOrCode) { //obviously pseudo
  document.getElementById("price").value = "your corresponding price to the code"
 }
}
  • This is all checked dynamically and before you hit the submit button.

With this said I would strongly recommend Angular.js since it supports databinding (example - databinding )

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

4 Comments

is it okay to just implement the script into the header or is into an external .js file better (elegant)?
hmm.. i cannot get it to work. im kind of a js newbie. i'm having my conditions and implement it into my body. but no changes at all
i meant header. sorry.
1. Generally one it's more elegant to have the script in a different file. But when websites launch their sites they tend to "minimize" their html, css and js in a huge file so that the client doesn't hafto make multiple requests to the backend. 2. Could you give me a code dump on fiddle or such? it's easier to review.
1

It is done with some jQuery code:

$('#form').submit(function() {
    $('#price').val(value_you_want_to_change);
});

2 Comments

this should change the value to value_you_want_to_change after submit or?!
the value changed before submit

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.