Robs
Robs

Reputation: 121

Backbone.js Change Text based on selected dropdown

I am trying to get some text to change when the user selects a different option in a dropdown box. I am using Backbone.js and am trying to figure out how to get the text belonging to the track when the user chooses an option.

As a follow-up, if I didn't set the value of the option to track.text, but rather track.title, how would I be able to get it from the .js file? I had originally set track.title and track.text in a JSON file. I am trying to learn how to get information from the JSON files. Thanks!!

window.LibraryLessonView = LessonView.extend({
events: {
    "change .sel " : "changeText"
},

changeText: function() {
//not sure what I should write here...
}


});

This is in my HTML file:

    <script type="text/template" id="lesson-template">
         <span class="lesson-title"><%= title %></span>
         <select class="sel">

//get the tracks from the JSON file and put them all in the dropdown
            <% _.each(tracks, function(track) { %>
              <option value = <%= track.title %> ><%= track.title %></option>


          <% }); %>        
          </select>

<p> Blah </P> //I want to change the text here

        </script>

Upvotes: 0

Views: 3783

Answers (1)

lukaleli
lukaleli

Reputation: 3627

[EDITED] that should work for you:

changeText: function(e) {
    alert(e.target.value); 
}

e.target refers to DOM element that triggers change event here. e.target.value returns value of currently selected item in select box

Upvotes: 3

Related Questions