Link to home
Start Free TrialLog in
Avatar of AHMED SAMY
AHMED SAMYFlag for Egypt

asked on

small help

i am not good in java

i have this code

i need to insert one value in the first input and auto calculate in the second and third input

<input type="text" name="markup" id="markup">
<input type="text" name="mark" id="mark">
<input type="text" name="marknet" id="marknet">

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
      $(document).ready(function(){
                                          
            var item1=$("#markup").val() / 100 ;
            var item2=$("#mark").val();
            var item3=item1 * item2;
            var item4=item3 + item2;
            $("#markup").change(function(){
                  $("#mark").val(item3);
                  $("$marknet").val(item4);
            });
      });
</script>
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Posting JavaScript questions in the Java TA is not appropriate, and might make your question less likely to be answered. http://technojeeves.com/joomla/index.php/free/127-javascript-is-not-java
I didn't check the whole thing, this however :
$("$marknet").val(item4);

Open in new window

should be :
$("#marknet").val(item4);

Open in new window

Avatar of AHMED SAMY

ASKER

i changed to it

please small help

<input type="text" name="markup" id="markup">
<input type="text" name="mark" id="mark">
<input type="text" name="marknet" id="marknet">

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
      $(document).ready(function(){
                                          
            var item1=parseInt($("#markup").val()) / 100 ;
            var item2=parseInt($("#mark").val());
            var item3=item1 * item2;
            var item4=item3 + item2;
            $("#markup").change(function(){
                  $("#mark").val(item3);
                  $("#marknet").val(item4);
            });
      });
</script>								

					

Open in new window



result is NaN
Well, Ahmed, the values you are attempting to set for items1, item2 are not initially set on documentLoad.

Give them an initial value in the HTML and you won't get the NaN:
<input type="text" name="markup" id="markup" value="100">
<input type="text" name="mark" id="mark" value="20">
<input type="text" name="marknet" id="marknet">

Open in new window


If you are supposed to be waiting to calculate the value, you should set item1-item4 in the .change. Can you try to explain a little better what you are trying to do here?
and you should put the calculation of your variables into the change event, else they will always stay static!

to handle NaN error, you could use isNaN in your codes:

<script type="text/javascript">
    $(document).ready(function () {

        $("#markup").change(function () {
            var item1 = parseInt(isNaN($("#markup").val()) ? 0 : $("#markup").val()) / 100;
            var item2 = parseInt(isNaN($("#mark").val()) ? 0 : $("#mark").val());
            var item3 = item1 * item2;
            var item4 = item3 + item2;
            $("#mark").val(item3);
            $("#marknet").val(item4);
        });
    });
</script>

Open in new window

thanks to all
this code is working dunamic but syntax to change this numbers to varaible is wrong
<input type="text" name="markup" id="markup">
<input type="text" name="mark" id="mark">
<input type="text" name="marknet" id="marknet">

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
      $(document).ready(function(){
                                          
            
            $("#markup").change(function(){

                  var x= 100;
                  var item1=200/ x ;
                  var item2=300;
                  var item3=item1 * item2;
                  var item4=item3 + item2;
			
                  $("#marknet").val(item4);
            });
      });
</script>	

Open in new window


how i can put this values from input to item1 and item2
The same way you initially did it:
var item1=parseInt($("#markup").val()) / x ;
var item2=parseInt($("#mark").val());

Open in new window

not working i tried it
ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i see it works right thanks

good work