0

I have two html elements on my page. One is dropdown and other is text field(which is working as autocomplete).

     <select id="match_engine_brand" name="match_engine[brand]" class="hidden-field"><option value="">Select Brand</option><option value="3">addidas</option>
        <option value="5">cat</option>
        <option value="2">nike</option>
        <option value="4">panther</option>
        <option value="6">tower</option></select>

while text field is

     <input class="string required ui-autocomplete-input" id="match_engine_shoe_model" name="match_engine[shoe_model]" placeholder="select model of shoe using autocomplete" required="required" size="50" type="text" autocomplete="off"> 

My cofeescript code is below

     $(document).ready ->

       $("#match_engine_brand").change ->
           window.flag_value =  $(this).val()
           alert(window.flag_value) #value display in alert

       $('#match_engine_shoe_model').autocomplete
         source: "/user/match_shoes/shoes?id="+window.flag_value
         select: (event, ui) -> $("#match_engine_shoe_model").val(ui.item.id)

In autocomplete function

window.flag_value       #give me undefined value    
$('#match_engine_brand :selected').val()   #give me undefined value 

How i can get dropdown value in autocomplete function.

Thanks for help

1 Answer 1

2

You need to have an initiated value of window.flag_value. Otherwise if you don't change #match_engine_branch, this var has no value. That's the reason of undefined value.

The way to solve it is to define this var before

$(document).ready ->
  window.flag_value = "something"
  ...

However I think it's unnecessary to use global var. Check it in function should work.

$('#match_engine_shoe_model').autocomplete ->
  dropdown_value = $("#match_engine_brand").val() ? "something"
9
  • dropdown_value = $("#match_engine_brand").val() || "something". This line in autocomplete give me syntax error. If i write this properly then my problem will be solved. please help Commented Jun 14, 2013 at 17:47
  • help me please i have describe my problem above Commented Jun 14, 2013 at 17:48
  • @Kashiftufail, I get used to Ruby's pipes equal and forget Coffee's special syntax :) Question mark is the right way to go. Sorry about that. Fixed.
    – Billy Chan
    Commented Jun 14, 2013 at 18:11
  • a = b || c is perfectly valid syntax...
    – Alex Wayne
    Commented Jun 14, 2013 at 18:13
  • 1
    @AlexWayne, thanks for mentioning that. I tried both of them in my console, d = a || "something" raised "Reference error: a is not defined", while d = a ? "something" actually returned "something". See coffeescript.org/#operators - The existential operators for details. I think ? is more suitable for this case as val() could be either undefined or null.
    – Billy Chan
    Commented Jun 14, 2013 at 18:21

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.