Today I have stuck in a problem where I have to select an item start with a specific character in a select box. I found the quiet short and interesting solution for this problem. I want to share this experience with you people.
There is a start with selector in jquery in this you can find out the item start with a specific character or string.
Start with Selector :- We use ^[Power sign] for this in jquery
Suppose you want item start with character C.You can find the value of that item like :
var Char='C';
var sValue = $("#SelectBoxId option[text^='"+ Char + "']").val();
Now select that specific item
$("#SelectBoxId option[value='"+ sValue + "']").attr("selected","selected");
I have found some more selector that may be useful for you.
End with Selector :- Use $ for end with selector
Suppose you have to find out the item which end with a specific character
var ='C';
var sValue = $("#SelectBoxId option[text$='"+ Char + "']").val();
Contains with selector :- Use * for contains with selector
Suppose you have to find out the item which contains a specific string like "rose".
var sValue = $("#SelectBoxId option[text*='rose']").val();
No comments:
Post a Comment