0

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>

1 Answer 1

3

[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

7
  • That is resulting in an undefined value being returned to me for some reason - probably because of what mu said.
    – Robs
    Oct 5, 2013 at 22:19
  • yes, my apologize. it was a while since I was working with Backbone. Now I have edited my answer.
    – lukaleli
    Oct 5, 2013 at 22:28
  • Thank you! Thats awesome. As a follow-up, if I didn't set the value of the option to track.text, 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.
    – Robs
    Oct 5, 2013 at 22:39
  • I don't really understand your question. Can you elaborate on that? PS. if my answer is correct please mark it as correct :)
    – lukaleli
    Oct 5, 2013 at 22:44
  • Well, I originally set the value of option to the text field of track (track.text). If I didn't do that, I was wondering how I would be able to get track.text and change the value of Blah in the <p> tag to track.text, which is located in the JSON file
    – Robs
    Oct 5, 2013 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.