0

I have a 2 files. file1 and file2. In file1 i have input box. so, i am getting this value in a javascript function like this

     <td>
         <input  onmouseleave="detect(this.value)" id="myqty"  value="'.$items['qty'].'">
     </td>
     <script>
            var retVal; 
            function detect(value)
            {

                 retVal=value;
                 return retVal

             }
       </script>  

I am returning that value. So in file2 i have javascript where i want to receive that returned value. how can i?

file2

 $(document).on('mouseleave', '#myqty', function(e){
  e.preventDefault();
   var returnValue= detect();
});

but in var returnValue= detect(); i am getting nothing.

8
  • 2
    Since you are using JQuery, you can get the required value as $("#myqty").val() Commented Aug 1, 2017 at 6:39
  • i want get the value of returned value of javascript function. how can i? Commented Aug 1, 2017 at 6:40
  • You aren't doing any processing with that value, so why not use $("#myqty").val() whenever you want the input value? Commented Aug 1, 2017 at 6:41
  • But you have not done any change in function so its better to take it using $("#myqty").val() Commented Aug 1, 2017 at 6:41
  • javascript can't get something from a different web page directly like that at all Commented Aug 1, 2017 at 6:41

3 Answers 3

1

Maybe its because function is expecting a parameter. Try this instead:-

$(document).on('mouseleave', '#myqty', function(e){
  e.preventDefault();
   var returnValue= detect($(this).val());
});
Sign up to request clarification or add additional context in comments.

Comments

0

You should use class and this how your code can be tweaked and updated:

$(document).on('mouseleave', '.myqty', function(e) {
  e.preventDefault();
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td>
  <input class="myqty" value="123">
</td>
<td>
  <input class="myqty" value="324">
</td>
<td>
  <input class="myqty" value="36546">
</td>
<td>
  <input class="myqty" value="654656">
</td>

Comments

0

You need to pass parameter in here var returnValue= detect();

First you dont need 2 functions for this. Why are you using 2 functions for this. You want quantity in another js file you can do all the code there only.

 $(document).ready(function(){
        $('#myqty').mouseleave(function(){

                var retVal= $('#myqty').val();
                alert(retVal);
        });
 });

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.