Jasomo Jasomo - 2 years ago 79
Javascript Question

Change option on button click

I am trying to write a userscript that will add buttons underneath a dropdown and change the value based on what button is clicked. There are 3 separate dropdowns given here:

<div style="float: left;">
<div><label>Number of big seeds</label> <select class="form-control" name="big"> <optgroup label="big"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></optgroup> </select></div>


<div><label>Number of small seeds</label> <select class="form-control" name="small"><optgroup label="small"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></optgroup> </select></div>


<div><label>Number of tiny seeds</label> <select class="form-control" name="tiny"><optgroup label="tiny"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option> </optgroup> </select></div>


And here is my failed attempt at a userscript:

$(document).ready(function() {

//Variables for buttons
var $0 = $('<button type="button" class="0" id="0">0</button>');
var $1 = $('<button type="button" class="0" id="1">1</button>');
var $2 = $('<button type="button" class="0" id="2">2</button>');
var $3 = $('<button type="button" class="0" id="3">3</button>');
var $4 = $('<button type="button" class="0" id="4">4</button>');
var $5 = $('<button type="button" class="0" id="5">5</button>');

var $6 = $('<button type="button" class="1" id="0">0</button>');
var $7 = $('<button type="button" class="1" id="1">1</button>');
var $8 = $('<button type="button" class="1" id="2">2</button>');
var $9 = $('<button type="button" class="1" id="3">3</button>');
var $10 = $('<button type="button" class="1" id="4">4</button>');
var $11 = $('<button type="button" class="1" id="5">5</button>');

var $12 = $('<button type="button" class="2" id="0">0</button>');
var $13 = $('<button type="button" class="2" id="1">1</button>');
var $14 = $('<button type="button" class="2" id="2">2</button>');
var $15 = $('<button type="button" class="2" id="3">3</button>');
var $16 = $('<button type="button" class="2" id="4">4</button>');
var $17 = $('<button type="button" class="2" id="5">5</button>');

//Add buttons


//Click event
var thisClass = parseInt($(this).attr('class'));
var thisID = parseInt($(this).attr('id'));


Can someone tell me where I'm going wrong?

Answer Source

You're binding the click event immediately, but not adding the buttons until the document is ready. Put the .click() call inside $(document).ready() after it appends the buttons.

$(document).ready(function() {

    //Variables for buttons
    var $0 = $('<button type="button" data-index="0" data-value="0">0</button>');
    var $1 = $('<button type="button" data-index="0" data-value="1">1</button>');
    var $2 = $('<button type="button" data-index="0" data-value="2">2</button>');
    var $3 = $('<button type="button" data-index="0" data-value="3">3</button>');
    var $4 = $('<button type="button" data-index="0" data-value="4">4</button>');
    var $5 = $('<button type="button" data-index="0" data-value="5">5</button>');

    var $6 = $('<button type="button" data-index="1" data-value="0">0</button>');
    var $7 = $('<button type="button" data-index="1" data-value="1">1</button>');
    var $8 = $('<button type="button" data-index="1" data-value="2">2</button>');
    var $9 = $('<button type="button" data-index="1" data-value="3">3</button>');
    var $10 = $('<button type="button" data-index="1" data-value="4">4</button>');
    var $11 = $('<button type="button" data-index="1" data-value="5">5</button>');

    var $12 = $('<button type="button" data-index="2" data-value="0">0</button>');
    var $13 = $('<button type="button" data-index="2" data-value="1">1</button>');
    var $14 = $('<button type="button" data-index="2" data-value="2">2</button>');
    var $15 = $('<button type="button" data-index="2" data-value="3">3</button>');
    var $16 = $('<button type="button" data-index="2" data-value="4">4</button>');
    var $17 = $('<button type="button" data-index="2" data-value="5">5</button>');

    //Add buttons

    //Click event
         var thisIndex = $(this).data('index');
         var thisValue = $(this).data("value");


You also shouldn't give duplicate IDs to elements. I've changed it to use a data attribute. And while it's OK to duplicate classes, using them just to hold data about the element is not really appropriate, so I've changed them to data attributes as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download