Giang Nguyen
Giang Nguyen

Reputation: 485

How to put 2 onchange=" " in <select>

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

Answers (2)

Oriol
Oriol

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

Tomas M
Tomas M

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

Related Questions