0

I've used jquery to get an "id" as a value for an input according to a dropdown value. Now the problem is, I want to use the 'id' to insert value to some other inputs. I don't know which event method to be used. For testing, I used the keyup method to check if the code works, when I manually type in a number, it works. So the problem is with the event method I choose. Please give me some suggestions.

    //Get id
     $(document).ready(function() {
        $('#salary_eid').change(function() {
            var sid = $(this).val();
            var data_String = 'sid=' + sid;
            $.get('search1.php', data_String, function(result) {

                $.each(result, function(){
                  $('#can').val(this.c_id);
          
                    
                });
            });       
          });

      //display other information
      $('#can').change(function() {
            var id = $(this).val();
            var data_String1 = 'id=' + id;
            $.get('search.php', data_String1, function(result1) {

                $.each(result1, function(){
                  $('#morning_rate').val(this.cMorning);
                    $('#night_rate').val(this.cNight);
                    $('#ot_rate').val(this.clientOt);
                    
                });
            });
        });
    });

 <label>Name</label>
 <input type="text" name="salary_eid" />
 <select id="salary_eid">
  <option>opt1</option>
  <option>opt2</option>
 </select>
    
 <input type="hidden" name="can" id="can" class="form-control" >
5
  • I am not too sure on what you are trying to achieve however it sounds like the on "change" event might do what you are looking for? Commented Jul 11, 2021 at 17:11
  • change event doesn't work as well @User1010 Commented Jul 11, 2021 at 17:13
  • Are you able to provide some clarity on your problem? Commented Jul 11, 2021 at 17:14
  • Im able to display the id on the #can input but from there I cant use the ID which I get from the first jquery code to do the second code. Commented Jul 11, 2021 at 17:24
  • In the dropdown, a name will be chosen, then it should get Id from a different table and display it on #can input which is hidden, then using the #can, I should be able to display other information. So all this should happen when I chose the name Commented Jul 11, 2021 at 18:24

1 Answer 1

0

You can trigger the keyup() event from the id code, this will then work through a manual or jQuery edit,

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>  
    //Get id
    $(document).ready(function() {
        $('#salary_eid').change(function() {
            var sid = $(this).val();
            $('#can').val(sid).keyup();
        });       
    });
    
    //display other information
    $(document).on('keyup', '#can', function() {
        console.log($(this).val())
    });
</script>

<label>Name</label>
<input type="text" name="salary_eid" />
<select id="salary_eid">
    <option>opt1</option>
    <option>opt2</option>
</select>

<input type="text" name="can" id="can" class="form-control" />

If you are going to hide the second input then I might suggest using a function instead without an input,

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script>  
    //Get id
    $(document).ready(function() {
        $('#salary_eid').change(function() {
            var sid = $(this).val();
            display_info(sid)
        });       
    });
    
    //display other information
    function display_info(value) {
        console.log(value)
    };
</script>

<label>Name</label>
<input type="text" name="salary_eid" />
<select id="salary_eid">
    <option>opt1</option>
    <option>opt2</option>
</select>
Sign up to request clarification or add additional context in comments.

3 Comments

But how can I do it without manually typing in the value for the #can input? The input will be hidden. so when I choose a name, it should automatically get the id to #can input then use the id to display information.
If the field is to be hidden then why use a different field? Why not trigger a function from the id function?

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.