Reputation: 485
I have a select option tag and i would like to put two onchange="" in the same tag but when i choose an option, it only work for: onChange="window.location.href=this.value" --> Show selected option on the url
But it did not work for: onchange="sortResult(this.value)".
If this is not allowed, then how can I achieve this?
<select name="sortby" class="form-control" id="city" onChange="window.location.href=this.value" onchange="sortResult(this.value)">
<option>1</option>
<option>2</option>
...
</select>
Upvotes: 0
Views: 1121
Reputation: 288500
Better avoid event handler content attributes and add the event handler using addEventListener
:
document.getElementById('city').addEventListener('change', function() {
window.location.href = this.value;
sortResult(this.value);
});
However, if you change window.location.href
, you will unload the page, so sortResult
may not be called.
Upvotes: 1
Reputation: 7343
Just use semicolon to separate it:
onChange="window.location.href=this.value; sortResult(this.value)"
So your HTML will read as:
<select name="sortby" class="form-control" id="city" onChange="window.location.href=this.value; sortResult(this.value)">
<option>1</option>
<option>2</option>
...
</select>
If you wish to manipulate the part of URL which is after hashtag #, use location.hash=this.value
instead of location.href
Upvotes: 1