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>
AHMED SAMYownerAsked:
Who is Participating?
 
Eddie ShipmanAll-around developerCommented:
Look, the problem is, you are trying to use the value from $("#mark") when it HAS NO VALUE. If you give it a value OR change the .change handler to only fire when it is changed then it will work.

This is setting a value for the mark input:
<input type="text" name="markup" id="markup">
<input type="text" name="mark" id="mark" value="300">
<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=parseInt($("#markup").val()) / x ;
            var item2=parseInt($("#mark").val());
            var item3=item1 * item2;
            var item4=item3 + item2;
            
            $("#marknet").val(item4);
        });
    });
</script>

Open in new window

This is changing the handler:
<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(){
        $("#mark").change(function(){
            var x= 100;
            var item1=parseInt($("#markup").val()) / x ;
            var item2=parseInt($("#mark").val());
            var item3=item1 * item2;
            var item4=item3 + item2;
            
            $("#marknet").val(item4);
        });
    });
</script>

Open in new window

0
 
CEHJCommented:
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
0
 
M. TariqApplication DeveloperCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
AHMED SAMYownerAuthor Commented:
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
0
 
Eddie ShipmanAll-around developerCommented:
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?
0
 
Ryan ChongCommented:
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

0
 
AHMED SAMYownerAuthor Commented:
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
0
 
Eddie ShipmanAll-around developerCommented:
The same way you initially did it:
var item1=parseInt($("#markup").val()) / x ;
var item2=parseInt($("#mark").val());

Open in new window

0
 
AHMED SAMYownerAuthor Commented:
not working i tried it
0
 
AHMED SAMYownerAuthor Commented:
i see it works right thanks

good work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.