Well my first post could be a doozy or it could be something so simple that it will make the pros eyes roll. So let me start by saying that I have "tinkered" with javascript as little as possible, but now I want to make an app that is mobile/responsive and so I MUST learn more javascript. My code practices with js are probably going to be atrocious so please feel free to offer tips/pointers there as well.
The app I am developing creates an "Army List" of models for players to use in a miniatures game. I have worked out some of the mechanics in a simple php/jquery setup (like adding html blocks and removing them), but now I am at the stage where I am trying to populate data. I have the fixed select boxes working fine (at least they seem to be for now) but the dynamic stuff is evading me like I were the plague.
So a couple quick things about this part of the app...
To build your army list, you add models to the form - I have chosen to use select boxes for this purpose since the models really are limited to what is available in the game. A player can have any number of troops, as the limit on the size of the army is not the number of models, but the total points that all of the models in the army add up to. For example, a 1000pt Army may consist of 20 models for one "faction", but may have 35 models when created using models from another "faction" or even a different army configuration with the faction chosen for the 20 model army. Of course for any army you need a leader - which was easy to do, that leader controls the first "Troop" which is called "Troop A" in the application and is required to "build" your army. All of the other models added to the army are broken down into other "Troops", each one requiring a "leader" (which may be a Warlord, Captain, or Sergeant - some of which are unique and can only be added to the army once), and the troop can contain any number of models up to the limit of the models that can be "commanded" by the selected leader model. (15 is the max for any leader in the game at the moment.)
So the problem I am having right now is the select box generated by my javascript code populates and creates the list, but it looks like any standard dropdown - not like the rest of the dropdowns on the jQuery Mobile page. I have tried altering the jqm tags in the javascript code, but that doesn't seem to have any effect.
- HTML CODE:
<div data-role="content">
- <!-- Troops/Models added to the Army -->
- <div class="troops">
- <div class="troop a required">
- <legend class="troop"><div class="troop-title">Troop A </div><div class="troop-points">[000 pts]</div></legend>
- <div><span>Leader for Troop A is the Army Leader/Warlord</span></div>
- <form id="select-troop-models-a" method="post" name="select-troop-models-a">
- <div data-role="fieldcontain">
- <label for="select-troop-models-a" class="select">Model:</label>
- <select class="bloc" name="select-troop-models-a" id="select-troop-models-a" data-native-menu="false" data-mini="true">
- <option value="choose-one" data-placeholder="true">Choose one...</option>
- <optgroup label="Leader Models">
- <?= $leader_id_select ?>
- </optgroup>
- <optgroup label="Elite Models">
- <?= $elite_id_select ?>
- </optgroup>
- <optgroup label="Solo/Monster Models">
- <?= $solo_id_select ?>
- </optgroup>
- <optgroup label="Soldier Models">
- <?= $soldier_id_select ?>
- </optgroup>
- </select>
- </div><!--/data-role-fieldcontain -->
- </form>
- <div><input type="hidden" value="1" id="lastTroopCount" name='lastTroopCount' /></div>
- <div><p><input id="addSlot" name="add-troop-slot" type="button" data-mini="true" value="Add Troop Slot" /></p></div>
- </div><!-- /troop a required -->
- </div><!-- /troops -->
- <div><p><input id="addTroop" name="add-troop" type="button" value="Add Another Troop" /></p></div>
- </div><!-- /data-role-content -->
All is good so far, except with the "dynamically" created "Troop" blocks, the jQuery Mobile "theme" seems to get broken when using the following method to bring those select strings into the javascript code:
- <div id="wac-leader-select" style="display:none'">
- <?= htmlspecialchars($leader_id_select); ?>
- </div>
- The javascript to read this is as follows:
- var div = document.getElementById("wac-leader-select");
- var leader_id_select = div.textContent;
- here is the whole function routine that creates the dynamic html block:
- $(function() {
- $('#addTroop').click(function(e) {
- e.preventDefault();
- thisTroop++;
- var thisTroopName = troops.charAt(thisTroop);
- var div = document.getElementById("wac-leader-select");
- var leader_id_select = div.textContent;
- var newTroop = $(["<div class='troop " + thisTroopName + "'>",
- "<legend class='troop'><div class='troop-title'>Troop " + thisTroopName.toUpperCase() + " </div>", "<div class='remove-troop-button'><button name='remove-troop' class='remove-troop-button' value='troop-" + thisTroopName + "'>[remove]</button></div>", "<div class='troop-points'>[000 pts]</div></legend>", "<div class='choose-troop-leader'><span>You must choose the leader for this troop before assigning other models</span></div>", "<div class='choose-troop-model' style='display:none;'><span>You may now add up to X models to this Troop</span></div>",
- "<form id='select-troop-models-" + thisTroopName + "' method='post' name='select-troop-models-" + thisTroopName + "'>",
- "<div data-role='fieldcontain'>",
- "<label for='select-troop-models-" + thisTroopName + "' class='select'>Model:</label>",
"<optgroup label='Leader Models'>" + leader_id_select + "</optgroup>",
- "</select></form>",
- "</div><!-- /troop block ends -->"
- ].join("\n"));
- $('div.troops:last').append(newTroop);
- $('#lastTroopCount').val(thisTroop);
- });
- });
Placing the model string select variable in a hidden div seems to work in transferring the data string, but it does appear in the display when you select from any one of the other custom selects boxes on the main page (panel).
Any help would be appreciated! :)