Reputation: 41
<select name="vehicle_make" onchange="test1(this.value)" style="width: 130px; float:left;" id="vehicle_make" class="home_input">
<option value="">Choose Make</option>
<option value="2063">Acura</option>
<option value="2064">Honda</option>
</select>
i have a dropdown. i have read the href attribute of the link and assigned it as the value to dropdown. but i want to take only the number from the link and assign it as value to dropdown how can i do it.
<select name="vehicle_make" onchange="test1(this.value)" style="width: 130px; float:left;" id="vehicle_make" class="home_input">
<option value="">Choose Make</option>
<option value="http://store.teknotik.com/category-s/2063.htm">Acura</option>
<option value="http://store.teknotik.com/category-s/2064.htm">Honda</option>
</select>
i have used following jquery to get the link as value
$("#data a").each(function(index) {
if ($(this).attr("class") == "subcategory_link") {
//document.getElementsById("year").innerHTML="<option value="+$(this).attr("href")+">test</option>";
lnk[cnt] = $(this).attr("href");
cnt = cnt + 1;
//alert($(this).attr("href"));
}
});
but i want to have the dropdown value as:
<select name="vehicle_make" onchange="test1(this.value)" style="width: 130px; float:left;" id="vehicle_make" class="home_input">
<option value="">Choose Make</option>
<option value="2063">Acura</option>
<option value="2064">Honda</option>
</select>
i want to have only the number before .htm in above dropdownho send as value. Please advise.
Upvotes: 3
Views: 1043
Reputation: 150253
Easy way:
$('#vehicle_make option[value!=""]').each(function() {
this.value = this.value.replace(/\D/g, "");
});
\d // Numerical char
\D // Not numerical char <===
g // Is a flag which means find all the occurrences, and not only the first.
So I'm looking for all the non numerical chars (with \D
)and "replace" them with an empty string.
Upvotes: 3
Reputation: 5393
try the .replace('', '')
function
.replace('http://store.teknotik.com/category-s/','').replace('.htm','')
should leave you with just the numerical value
Upvotes: 0
Reputation: 100175
Try:
$(document).ready(function() {
$("select[name='vehicle_make'] option[value!='']").each(function() {
this.value = this.value.split("/").pop().split(".")[0];
});
});
Hope it helps
Upvotes: 0
Reputation: 15550
You can find it like below ;
var url = $(this).attr("href");
var urlTemp1 = url.split("/");
var urlTemp2 = (urlTemp1[urlTemp1.length -1]).split(".");
var yourNumber = urlTemp2[0];
alert(yourNumber);
Upvotes: 0
Reputation: 13667
Seems like there are two parts here ... getting just the number you want:
lnk[cnt] = $(this).attr('href').substring($(this).attr('href').lastIndexOf('/')+1);
lnk[cnt]=lnk[cnt].replace('.htm','');
(not the most elegant solution, but should work)
Then setting the value:
$("#vehicle_make").val(lnk[cnt])
Upvotes: 0