Reputation: 29573
I want the user to click on a button called select. This then selects an option in the dropdownlist. The option it selects must be the one that starts with the first 10 characters of the text in the text box.
This is the dropdown and textbox.
<asp:DropDownList runat="server" ID="selDealer"></asp:DropDownList>
<a href="#" class="RemoveLink" >Select</a>
Dealer Name: <asp:TextBox ID="RtnDealerName" runat="server"></asp:TextBox>
At the moment the jscript only does a simple alert to test the select link is working.
<script language="javascript" type="text/javascript">
$(function () {
$(".RemoveLink").click(function () {
alert("Yes");
});
});
</script>
What do i put in the script to select the dropdown option which starts with the first 10 characters of the text in the text box.
Upvotes: 0
Views: 392
Reputation: 22468
$(".RemoveLink").click(function() {
var regexpToSearch = new RegExp( $("#<%= RtnDealerName.ClientID %>").val().substring(0,10), 'i');
var optionToSelect = $("#<%= selDealer.ClientID %> option").filter(function(index) {
return this.innerText.search(regexpToSearch) != -1;
}).first();
if(optionToSelect.length != 0){
$("#<%= selDealer.ClientID %>").val(optionToSelect.val());
}
});
To check a last ten characters on textbox against last ten characters of an option change expression to create regexpToSearch as below:
var selDealerText = $("#<%= RtnDealerName.ClientID %>").val();
var regexpToSearch = new RegExp(selDealerText.substring(Math.max(selDealerText.length - 10, 0)) + '$', 'i');
Upvotes: 1