JavaScript Displaying content based on radio button selection

I’m pretty new to backbonejs and i’m trying to create a basic application.

The application is something like this:

I have 5 sections: A, B, C, D and E

Each section has 2 radio buttons.

Section A - Radio1, Radio2
Section B - Radio3, Radio4
Section C - Radio5, Radio6
Section D - Radio7, Radio8
Section E - Radio9, Radio10

Depending on what radio button is selected, I need to display a section (previous sections must also display)

I have had a look at maybe using a model to determine which radio was selected and also what section is displayed. Is this the correct approach?

Answer:1
var State = Backbone.Model.extend({
    defaults: {
        id: null,
        name: "",
        isOn: false
    }
});
var Section = Backbone.View.extend({
    model: State,
    events: {
        'change [type="checkbox"]': function (event) {
            var $checkbox = $(event.target);
            this.model.set("isOn", $checkbox.is(":checked"));
            this.model.get("dispatcher").trigger("toggle", this.model.get("id"));
        }
    },
    initialize: function (options) {
        this.listenTo(this.model, "change:isOn", function (model, isOn) {
            if ( isOn ) {
                this.$el.find(".section").show();
                this.$el.find("input").prop("checked", true);
            }
            else {
                this.$el.find(".section").hide();
                this.$el.find("input").prop("checked", false);
            }
        });
        this.listenTo(dispatcher, "toggle", function (id) {
            if ( this.model.get("id") < id ) {
                this.model.set("isOn", true);
            }
            if ( this.model.get("id") > id ) {
                this.model.set("isOn", false);
            }
        });
    },
    render: function () {
        this.$el.html('<div>' + this.model.get("name") + '</div><input type="checkbox"><div class="section" style="min-height:100px; background-color:grey; display:none;"></div>');
        this.$el.appendTo("body");
    }
});
var dispatcher = _.extend({}, Backbone.Events);
_.each([
    {id: 1, name: "A", isOn: false, dispatcher: dispatcher},
    {id: 2, name: "B", isOn: false, dispatcher: dispatcher},
    {id: 3, name: "C", isOn: false, dispatcher: dispatcher},
    {id: 4, name: "D", isOn: false, dispatcher: dispatcher},
    {id: 5, name: "E", isOn: false, dispatcher: dispatcher}
], function (item) {
    var view = new Section({model: new State(item)});
    view.render();
});

I didn't understand the meaning of two radio.. so I used checkbox per section.. Hope this will uncover some basics of backbone.

Answer:2

Yes this approach will work. I recommend if you need to communicate between views to use models via events - this will generally result in better architecture (your views will be more decoupled).

You can react to the change event in the view (using the events hash) and update an attribute on a model for each group, e.g. this.model.set('termsAccepted', true/false) then as long as the other view(s) have access to this model you can react to the change event of that attribute, e.g. this.listenTo(this.model, 'change:termsAccepted', this.onTermsAcceptedChange).

Answer:3

There may be a very simple solution to your objective. If you monitor the radio-button state via a javascript function, then you can use that function to change a class statement for the parent div. In CSS, you can then define actions to hide or display a div based on it's class. This would only take a few lines of javascript and a few lines of CSS. For example, this example in codepen shows a way to accomplish the hide/show for a variably sized div. The number of divs in this approach is arbitrary - you can have as many or few as you like, and the only action required of the user is to click on the header to expand or collapse the associated div.
In this example, clicking on the header acts as a toggle to expand or collapse the associated div. The example is set up so that only one div is expanded at a time and clicking on a different header automatically collapses any open div so that only one div is open at a time. If you do not want that behavior, just remove the for loop in the accOff() function.

Answer:4

Simply, i'm trying to use jQuery to append a numeric value (currently stored in an array) to the end of the "id" attribute of a number of specified elements. $('#headerAfirstLink, #headerAsecondLink, ...

Simply, i'm trying to use jQuery to append a numeric value (currently stored in an array) to the end of the "id" attribute of a number of specified elements. $('#headerAfirstLink, #headerAsecondLink, ...

  1. append array value in php
  2. append array value in javascript
  3. append array value in jquery
  4. append array value python
  5. append array value
  6. append array value js
  7. append array values swift
  8. append array values numpy
  9. append value array bash
  10. adding array value in php
  11. append values array java
  12. adding array values in javascript
  13. adding array values in java
  14. adding array values
  15. adding array values python
  16. adding array values in c#
  17. append array key value php
  18. c append array value
  19. python append array values to list
  20. vba array append value

I'm using the jQuery .append() function to input content into HTML elements via their id like so; function returnGameDetailed(data) { $('#game-synopsis').append(data.results.deck); } For ...

I'm using the jQuery .append() function to input content into HTML elements via their id like so; function returnGameDetailed(data) { $('#game-synopsis').append(data.results.deck); } For ...

  1. jquery append multiple divs
  2. jquery append multiple lines
  3. jquery append multiple items
  4. jquery append multiple html elements
  5. jquery append multiple times
  6. jquery append multiple options to select
  7. jquery append multiple input
  8. jquery append multiple lines of html
  9. jquery append multiple
  10. jquery append multiple td to tr
  11. jquery append multiple attributes
  12. jquery append multiple rows to table
  13. jquery append multiple li to ul
  14. jquery append multiple objects
  15. jquery append multiple classes
  16. jquery append multiple variables
  17. jquery append multiple nodes
  18. jquery append multiple options
  19. jquery append multiple copies
  20. jquery append multiple attr

Issue happens when I upload multiple files. Let's say I upload 7 files, some times it only takes 2 - other times all 7 files. I am not expecting any return yet, just using dropzone and move the file ...

Issue happens when I upload multiple files. Let's say I upload 7 files, some times it only takes 2 - other times all 7 files. I am not expecting any return yet, just using dropzone and move the file ...

  1. dropzone upload multiple files
  2. dropzone upload multiple false
  3. dropzone upload multiple files at once
  4. dropzone upload multiple not working
  5. dropzone upload multiple
  6. dropzone upload multiple example
  7. dropzone upload multiple files php
  8. dropzone upload multiple true
  9. dropzone multiple upload php
  10. dropzone.js upload multiple files
  11. react dropzone upload multiple files
  12. dropzone.js upload multiple
  13. dropzone disable multiple upload
  14. dropzone js multiple file upload php
  15. dropzone js multiple file upload example

I have been working on this problem for a while now. I need some major guidance. I have a progress bar that is a made up of unordered list <ul></ul> with list items inside that make up ...

I have been working on this problem for a while now. I need some major guidance. I have a progress bar that is a made up of unordered list <ul></ul> with list items inside that make up ...