1

I have a simple jquery function that reflects the selected value of a dropdown to textbox but the problem is that the console says that my function was undefined. I put the function inside the onchange event of the dropdown. Please note that all of my jquery functions was on the bottom of the page..

and here is the dropdown:

<div class="form-group">
      <label class="control-label">Operation</label>
        <select class="form-control" id="operationName" name="operationName">
       <option value="">-SELECT-</option>
        <?php
         foreach($operation as $val)
         {
          ?>
          <option value="<?php echo $val['OperationName'];?>"><?php echo $val['OperationName'];?></option>
          <?php
         }
      ?>
      </select>
          <span class="text-danger"><?php echo form_error('operation');?></span>
          <input type="text" class="form-control" id="deldays" name="deldays" 
          value="" />

this is the operation array model

function getDeliveryDays($str) { $this->db->select('DeliveryDays'); $this->db->from('operation'); $this->db->where('OperationName',$str); return $this->db->get()->result(); }

this is the controller:

public function getDays()
{
    $id = $this->input->post('q');
    $data['days'] = $this->wip_model->getDeliveryDays($id);

    echo json_encode($data);
}

I took the working in my other project and use it in my current project but still the same result and when I inspect it in the dev tools, the said script is missing in view source but it's present in IDE:

this is the code:

function showDeliveryDay(operation)
    {
        $.ajax({
            type: "POST",
            url: "<?php echo site_url('wip/getDays/');?>",
            data: {q:operation},
            success: function(data){
                console.log(data);
            },
        });
    }

enter image description here

Thanks in advance....

1
  • what do you mean by before html? I put all of my jquery functions at the bottom of the page before the closing body. Commented Feb 24, 2016 at 3:57

3 Answers 3

1

Does you put the function inside domready? If so, remove that and placed it outside like so :

<script>
  function showDays() {
     var x = document.getElementById("operationName").value;
     document.getElementById("deldays").innerHTML = x;
  }

 // domready here
 $(function(){...}); // or $(document).ready(function(){...});     
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

I tried that dom ready but same results still prompts that the function is undefined that's why I revert to simple script but still the same outcome...
So, you tried already put outside ondomready? Usually those errors come from that case. Can you reproduce code on jsfiddle.net
I just followed this tutorial in w3schools.com/jsref/event_onchange.asp
That tutorial working great. Can you post the whole js code between <script></script> tag that you make so far.
I tried the code of WsdmLabs but it display nothing on the textbox and also I am using the new version of jquery library jquery-1.12.0.min.js if it does affect the whole process....
1

Make sure you are including your jquery in your code. try this:

jQuery( document ).ready(function() {    
	jQuery( "#operationName" ).change(function() {
     var pVal = jQuery("#operationName").val();
     jQuery("#deldays").val(pVal);     
     });  

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label class="control-label">Operation</label>
<select class="form-control" id="operationName" name="operationName">
<option value="">-SELECT-</option>


<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>

</select>
<span class="text-danger"></span>
<input type="text" class="form-control" id="deldays" name="deldays" 
value="" />

</div>

12 Comments

I am using the new jquery library or i must use the one that you mention?
I tried the code and it's working if it's just as it is but I am loading my options value from database and when I modified the code with data from db, it's not working.....
@SilverRay: than plz show the actual code in your question plz
that's the actual code already that I post in my question... please see my poost....
@SilverRay: in your code plz remove onchange="showDays()" from select box
|
0

You can refer this code

Html

<div class="form-group">
      <label class="control-label">Operation</label>
        <select class="form-control" id="operationName" name="operationName">
       <option value="">-SELECT-</option>


          <option value="one">1</option>
               <option value="two">2</option>
                    <option value="three">3</option>

      </select>
          <span class="text-danger"></span>
          <input type="text" class="form-control" id="deldays" name="deldays" 
          value="" />

Js code

jQuery( document ).ready(function() {    
     jQuery('#operationName').on('change', function(){    
     var p=jQuery(this).val();
     jQuery("#deldays").val(p);     
     });   

});

You can view demo here

2 Comments

I tried you code no errors on console but it doesn't display anything in textbox....
I tried using your code with foreach and I am having an error but when I replace it with while loop it works perfectly using plain php but I don't know how when using codeigniter...

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.