3

I write ajax on my page, to get subcategories in select option, depending on categories option list click. In all browsers it works good, i can see my request, response in browsers console... but in chrome functions even doesn`t call. Dou you know, in what the problem is? Here is my code:

    <td>
      <span style="color: #898989;">Main categories</span>
      <br />
      <select style="width: 200px;">
        <?foreach ($main_categories as $item){?>
        <option onclick="get_sub_cat(<?=$item['id']?>,2);return false;" value="<?=$item['id']?>"><?=$item['title']?></option>
        <?}?>
      </select> 
    </td>
    <td>
      <span style="color: #898989;">Subcategories</span>
      <br />
      <select  name="sub_cat" style="width: 200px;" id="prod_subcat_2">
      </select> 
    </td>


function get_sub_cat(id, select_id){
$.ajax({
   type: "POST",
   url: "<?=base_url()?>admin/product/get_sub_cat/"+id,
   data: "",
   success:function (option_list) {
     $("#prod_subcat_"+select_id).children().remove();
     $('#prod_subcat_'+select_id).append(option_list);
   }
 });
}
6
  • If really javascript doesn't work at all in your chrome browser. you need to enable it. read this Commented Mar 13, 2012 at 11:49
  • I'd look for existing questions about how to use the debugging tools in Chrome. It's very easy to set a breakpoint in a function to see if it gets called, to see what calls are being made to the backend, etc. Commented Mar 13, 2012 at 11:50
  • @gdoron: not everyone is native English speaker. It's perfectly clear what he means. Why didn't you simply edit the title? Commented Mar 13, 2012 at 11:53
  • @thg435. I'm not a"native English speaker" as well. I really don't know what was asked here. Can you please help me out? Commented Mar 13, 2012 at 11:54
  • 1
    @Anton You should post the generated client-side code, not the server-side code, since JavaScript runs client-side. Commented Mar 13, 2012 at 11:59

4 Answers 4

4

An <option> elements onclick is not universally supported, instead use an event of the parent <select>

$('#theselect').change(function() {
   alert( $(this).val() );
});​
Sign up to request clarification or add additional context in comments.

1 Comment

Ran into this issue this morning. This question, and this answer solve my problem.
2

1) If you run on local folder and not running on server, then chrome have security reasons not to run these type of javascript calls. There is multiple threads around stack overflow about running localy javascript ajax calls on chrome if thats the case.

2) Try your javascript with simple alert("hey"); to check if javascript is working or its the ajax.

3) If javascript works. Use google "Developer tools" ctrl+shift+i, set breakpoint on your javascript call and check what get passed as variable and wheres the problem.

4) If javascript dont work try this http://support.google.com/chrome/bin/answer.py?hl=en&answer=114662

Comments

0

Try attaching an onchange event handler and getting the selected element from the select element passed into on the event. It probably doesn't make sense to attach event handlers to individual options.

Comments

0

You need to add some quotes :

get_sub_cat(\"<?=$item['id']?>\",2)

you need to send this parameter as a string.

Are you sure your JavaScript is in a <script> tag ? the question doesnt show it

<script>
   // your javascript
</script>

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.