Alester Lewis
Alester Lewis

Reputation: 23

How can I reference an method in HTML that is in an object

I'd like to know how to reference a method in HTML that is in an object in my JavaScript file, or if that is even good practice.

HTML

<select class="ranking" name="rank" id="" onchange = "if(this.selectedIndex) Someobj.Search.chosenRank();">
    <option value="Last_Updated">Last Updated</option>
    <option value="Relevance">Relevance</option>
    <option value="Distance">Distance</option>
</select>
<script src="static 'main/js/search.js"></script>

JavaScript

Someobj.Search = {
    chosenRank: function(){
        alert("you chose" + this.selectedIndex );
    }
}

Upvotes: 0

Views: 98

Answers (1)

MarkSill
MarkSill

Reputation: 73

It's generally better to separate code from your HTML. I'd recommend something along the lines of:

document.querySelector(".ranking").addEventListener("change", Someobj.Search.chosenRank);

Upvotes: 1

Related Questions