When a user messages another user. They can pick which type of profile to send to. (Common or Manager)… I’m checking in the backend which profile to send it to with “recipient_type”, How do I get my autocomplete to choose the hidden radio button for me?
The autocomplete looks like this:
To: John Doe - Manager
or
To: John Doe
template:
<div class="hide">
<input type="radio" id="id_recipient_type" name="recipient_type" value="0" />
<input type="radio" id="id_recipient_type" name="recipient_type" value="1" />
</div>
<div class="inline-block">
<label for="id_omnibox"></label>
<input type="hidden" name="recipient_username" id="id_recipient_username" />
<input id="message-to" class="required input-text" style="width: 145%;"name="omnibox" placeholder="Search for user..." autocomplte="on" type="text" />
</div>
script:
$(document).ready(function(){
$.get('/autocomplete/message/', function(data) {
var completions = new Array();
var dict = JSON.parse(data, function(key, value) {
completions.push(key);
return value;
});
$('#message-to').autocomplete({
source: completions,
minLength: 1,
select: function(value, data){
$('#id_recipient_username').val(dict[data.item.value])
split_string = data.item.value.split("- ");
$('#id_recipient_type_'+(split_string[1]=="Manager"?"1":"0")).attr('checked', true);
}
});
});
});
It seems that in order to your code to work you need to change or:
Radio boxes IDs. or:
The jquery selector to
#id_recipient_type[value="1"]or#id_recipient_type[value="0"].I’d ratter use the first solution, since in html ids should be unique.
You need to solve a problem stated by kmfk with your split where it throws an error when don’t find the
' - 'string, so change:To: