Clickable Dropdown List Images - javascript

So, what I'm trying to do here is make a clicker game. I want the enemy to be selectable from a dropdown list, and then you can click whichever enemy you select to add to your resources (of which there are four for this game). However, when I use this code, the clickable images won't show up. Is there a more efficient way to do this? Here's the HTML and the Javascript from the sections I'm working on (the other sections have already been tested to see that they work). The images it's referring to are in the same folder and the onclick functions have already been written, and were tested with a non-dropdown list image and worked. The dropdown list worked for a non-clickable image. They just don't seem to work when I put them together.
function changeE(){
var dropdownList = document.getElementById('changeEid');
var selectedIndex = dropdownList.selectedIndex;
var selectedValue = dropdownList.options[selectedIndex].value;
var enemyDiv = document.getElementById('enemyHere');
switch(selectedValue){
case 'DestroyerPrincess':
enemydiv.innerHTML = '<img src="B1-DestroyerPrincess.png" width="445px" height="590px" onclick="fuelClick(1);ammoClick(1); steelClick(1); bauxiteClick(1)">';
break;
case 'LightCruiserDemon':
enemydiv.innerHTML = '<img src="B2-LightCruiserDemon.png" width="424px" height="616px" onclick="fuelClick(1);ammoClick(1); steelClick(1); bauxiteClick(1)">';
break;
}
};
Enemy
<select id="changeEid" onclick="javascript:changeE();">
<option value="DestroyerPrincess">Destroyer Princess</option>
<option value="LightCruiserDemon">Light Cruiser Demon</option>
</select>
<div id="enemyHere">
</div>

It's simply a typo. In JavaScript part of code, replace enemydiv.innerHTML by enemyDiv.innerHTML
function changeE(){
var dropdownList = document.getElementById('changeEid');
var selectedIndex = dropdownList.selectedIndex;
var selectedValue = dropdownList.options[selectedIndex].value;
var enemyDiv = document.getElementById('enemyHere');
switch(selectedValue){
case 'DestroyerPrincess':
enemyDiv.innerHTML = '<img src="B1-DestroyerPrincess.png" width="445px" height="590px" onclick="fuelClick(1);ammoClick(1); steelClick(1); bauxiteClick(1)">';
break;
case 'LightCruiserDemon':
enemyDiv.innerHTML = '<img src="B2-LightCruiserDemon.png" width="424px" height="616px" onclick="fuelClick(1);ammoClick(1); steelClick(1); bauxiteClick(1)">';
break;
}
};

Instead of onclick i suggest you to use onchange event:
<select id="changeEid" onchange="javascript:changeE();">
<option value="DestroyerPrincess">Destroyer Princess</option>
<option value="LightCruiserDemon">Light Cruiser Demon</option>
</select>
with select tags i always use onchange it's better because you only want changes to be made when the user selects something it does make sense , i hope this helps

Related

HTML JS How to display text about a dropdown list item

I have a programm where I want to have a dropdown list called DropDownGamer which i've made:
<select disabled="disabled" id="DropDownGamer">
<option disabled selected value>Valige Youtuber</option>
<option value="Terminats">Terminats</option>
<option value="HDTanel">HDTanel</option>
<option value="DeniedNetwork">DeniedNetwork</option>
<option value="Shroud">Shroud</option>
</select>
Now I have a checkbox that enables the dropdown list so you can choose from it. I want to make something like this: If you choose one of the items from the dropdown list, lets say you choose Terminats - the first item in the list, And press a button called ShowInfo, I want there to show up text below the button, about that item, something like this: Terminats has xxx subs and xxx views. I want to have custom text for each item.
I thought about something like this:
<input id="Button1" onclick="showInfo()" type="button" value="Show Info." />
function showInfo()
{
if (document.getElementById("Terminats"))
{
}
else if (document.getElementById("HDTanel"))
{
}
}
But I dont think that would work or atleast, I can't seem to get it to work.
Thanks!
one option would be to use a switch statement:
function showInfo() {
var selectElement = document.getElementById("DropDownGamer");
switch (selectElement.value) {
case 'Terminants':
// code to display text for terminants
break;
case 'HDTanel':
// code to display text for hdtanel
break;
etc...
}
}
best option would be to create an element after your button, let's say
<p id="description"></p>
and then something like
var description = document.getElementById('description');
description.removeChild(description.childNodes[0]);
var descriptionText = document.createTextNode("your description");
description.appendChild(descriptionText);
inside the respective case statement (you first target your element, then empty it if there is text inside, then create new text node and append it).
so full code would be:
var description = document.getElementById('description'),
selectElement = document.getElementById('DropDownGamer');
function showInfo() {
switch (selectElement.value) {
case 'Terminants':
description.removeChild(description.childNodes[0]);
var descriptionText = document.createTextNode("your description for terminants");
description.appendChild(descriptionText);
break;
case 'HDTanel':
description.removeChild(description.childNodes[0]);
var descriptionText = document.createTextNode("your description for hdtanel");
description.appendChild(descriptionText);
break;
etc...
}
}
I would put data attributes on each select item, and then use the function to grab that data. You could put subs and views right on the selected item with data- attributes like this
<option selected value="Valige Youtuber" data-subs="2" data-views="23">Valige Youtuber</option>
Then in your showInfo() function you could grab the select list, and find the datavalue
function showInfo() {
var dropdown = document.getElementById('DropDownGamer');
var dataset = dropdown[dropdown.selectedIndex].dataset;
var subs = dataset.subs;
var views = dataset.views;
}
Or even put the whole custom string into a data attribute like this
<option selected value="Valige Youtuber" data-customString="my custom statement">Valige Youtuber</option>

Option value doesnt showup in dropdown - jquery

I am trying to add new options in a dropdown based on the other dropdown value.
the new added option doesnt appear in the dropdown on my custom webkit browser.
When I try to debug it the values are present in the dropdown, just it doesnt show up in the front end.
I have attached the code but its working in jsbin :(
When I click the empty dropdown and then click New button value doesnt show up but if I dont click the empty dropdown and click new button directly values appear normally.
https://jsbin.com/kikicuhabo/1/edit?html,css,js,output
It is a good practice to write HTML in lower case, and to close the tags.
If you want to use jQuery I recommend to select elements alway with the "$('')" instead of mixing with the "document. ... "
Since yours it is a custom browser I would try a solution in Vanilla JS.
function AddToCB(p_oCB, p_sText) {
var oSelect = document.querySelector(p_oCB);
var iNewLast = oSelect.length;
var sDisplay = p_sText + (iNewLast + 1);
var oNewItem = document.createElement('option');
oNewItem.innerHTML = sDisplay;
oNewItem.setAttribute('value', sDisplay)
oSelect.appendChild(oNewItem);
oSelect.selectedIndex = iNewLast;
return iNewLast;
}
function AddOption() {
var select = document.querySelector("#cmb");
var text = select.options[select.selectedIndex].value;
AddToCB('#list', text);
}
<select name="cmbColor" id="cmb">
<option>AA</option>
<option>BB</option>
<option>CC</option>
</select>
<input type="button" class="float-right" VALUE="New" onClick="AddOption()" >
<select name="list" id="list">
</select>

JavaScript - dynamic link depending on multiple DropDown Selections or Checkboxes

New to Javascript. Here is the story:
I need to create a dynamic link to redirect to one of the 6 different courses, depending on the options chosen by the user. This can be done via 3 dropdown menus or via 3 sets of checkboxes:
img. 1: http://i.imgur.com/oi8WYYv.png
or
img. 2: http://i.imgur.com/tRIdxBv.png
I tried to combine codes from several examples from stackoverflow, but it's a mess... Any idea is highly appreciated.
[edit 06.04.2015]
Thanks for your suggestions, but the story is a little more complicated:
The links are fixed, i.e. they are quite complicated to write it via a "generate" function (they are something like: base_url + id_1 + base_url_2 + id_2 + base_url_3)
There are 6 different courses, each one with its own link:
a) regular user - in English b) regular user - in German c) user with access to confidential data - in English d) user with access to confidential data - in German e) user manager - in English f) user manager - in German
To select the proper course, you would need to meet criteria from more than one form. I think there should be a lot of "if" statements in place in order to select the proper course via the questions. Or the questions are not the correct ones...
This is fairly trivial; all you have to do is check the values of each select tag, and set the href of the anchor accordingly.
Here's a tiny mockup:
window.onload = function(){
var select = document.getElementById('language');
var anchor = document.getElementById('next');
select.onchange = function(){
anchor.href = select.value;
}
}
Which is your preferred language?
<select id="language">
<option value="#">--------------</option>
<option value="http://stackoverflow.com/questions/tagged/javascript">JavaScript</option>
<option value="http://stackoverflow.com/questions/tagged/python">Python</option>
<option value="http://stackoverflow.com/questions/tagged/c%2b%2b">C++</option>
</select>
<br>
<a id ="next" href="#">Next</a>
Basically, I'm listening to onchange on the select element, and can therefore update the link whenever the chosen option changes.
If you aren't using an <a> anchor tag, but something else, for the Next page button, you can always just check the values before redirecting the user:
window.onload = function(){
var select = document.getElementById('color');
var next = document.getElementById('next');
next.onclick = function(){
switch (select.value){
case '1':
window.location.href = "http://blog.xkcd.com/2010/05/03/color-survey-results/";
break;
case '2':
window.location.href = "http://www.omglasergunspewpewpew.com/";
break;
default:
break;
}
}
}
What is your least favourite colour?
<select id="color">
<option value="1">puke</option>
<option value="2">The best color in the world!</option>
</select><br>
<button id="next">Next</button>
Basically, whatever you're doing, you can just check select.value, and perform an action accordingly.
first draft, not working but that's the best I got so far:
<script type="text/javascript">
window.onload = function(){
var language = document.getElementById('lang');
var confidential = document.getElementById('conf');
var manager = document.getElementById('mgr');
var next = document.getElementById('next');
next.onclick = function(){
switch (true){
case language === "isEnglish":
window.open('link/regular_user_in_English', '_blank');
break;
case language === "isGerman":
window.open('link/regular_user_in_German', '_blank');
break;
case language === "isGerman" && confidential === "isConf":
window.open('link/conf_in_German', '_blank');
break;
default:
break;
}
}
</script>
Which language would you prefer to do the training in?
<select id="lang">
<option value="isEnglish">English</option>
<option value="isGerman">German</option>
</select><br />
Are you a confidential data handler?
<select id="conf">
<option value="notConf">No</option>
<option value="isConf">Yes</option>
</select><br />
Are you a people manager?
<select id="mgr">
<option value="notManager">No</option>
<option value="isManager">Yes</option>
</select>
<br />
<button id="next">Next</button>

Javascript multiple select boxes

I tried searching for an answer to this for a while, but to no avail.
I'm trying to do a simple online calculator (that calculates some photovoltaic panels energy), but I'm stuck in something simple (I'm new to Javascript although I worked with Flash's ActionScript 3.0 for a while).
What I need done is a html select that defines which other select group appears in the page. Something like this (obviously this doesn't work, just setting an example):
HTML
<html>
<body>
<select id="test1" onclick="checkField()">
<option>Selected A Group</option>
<option>Selected B Group</option>
</select>
<script>//insert second group here</script>
</body>
</html>
Javascript
function checkField(){
var temp = document.getElementById('test1').value;
if(temp === "Selected A Group"){
//insert code to "echo" the first optional select group
} else {
//insert code to "echo" the second optional select group
}
}
Sorry if its a bit confusing, but I cant really explain all that well.
Here is an example of what I would want, where selecting a option makes the other fields change accordingly: http://www.toshiba.eu/innovation/download_drivers_bios.jsp
you are almost there, actually javascript doesn't "echo" values directly, it does log values using console.log(your value); to a debug console, similar to AS2 trace() if my memory isn't failing.
To "output" information to the document you should have a look into document.write
When you use document.write it will directly write to the documents end.
The "correct" way would be to create a DOM element, with the elements you want inside it, and then append it to the desired element. Have a look at the comments
<!-- Be Aware to use the onchange trigger on select boxes, if you use onclick the function will run, even
if you didn't really chose any option -->
<select id="test1" onchange="checkField()">
<!-- Is good to have a first non-value option, better to trigger the onchange event, if you have
Select A Group as first option and you click on it, it didn't really "Change", you would have to
pick B Group and then A Group again to trigger the onchange event correctly. -->
<option value="">-- select an option --</option>
<!-- You can have a value attribute on the options, so it's easy to process when programming
while displaying a more detailed description to the users -->
<option value="A">Selected A Group</option>
<option value="B">Selected B Group</option>
</select>
<!-- We create an empty element where we are gonna place the new Select -->
<div id="newSelect"></div>
<!-- By Placing the Javascript on the end of <body>, we ensure that all the DOM elements loaded before running the script -->
<script>
function checkField(){
var newSelect = document.getElementById('newSelect'); //targeting container;
var temp = document.getElementById('test1').value;
//Some tasks we do always the option chose is not the first custom one, so we don't have to repeat it
//on the two If's below
if(temp !== ""){
// We remove the select if we placed one already before, so we can add the new one,
// For example if we chose B Group but changed our mind and Chose A Group later.
if(oldChild = newSelect.getElementsByTagName('select')[0]){
oldChild.remove();
}
var select = document.createElement("select");
select.setAttribute('id', 'newSelect');
}
if(temp === "A"){
//you could do JUST:
//body.innerHTML = "all the html you want in here" instead of all the code following;
//but all those code is supposed to be the "correct way" of adding elements to the HTML,
//Google a bit about that for detailed explanations
var option1 = document.createElement("option");
option1.value = 1;
option1.text = "Option 1";
var option2 = document.createElement("option");
option1.value = 2;
option2.text = "Option 2";
select.appendChild(option1);
select.appendChild(option2);
newSelect.appendChild(select);
} else {
var option1 = document.createElement("option");
option1.value = 3;
option1.text = "Option 3";
var option2 = document.createElement("option");
option1.value = 4;
option2.text = "Option 4";
select.appendChild(option1);
select.appendChild(option2);
newSelect.appendChild(select);
}
}
</script>
Of course there are ways to make this slightly shorter, using loops if your data to ouput has a pattern, but lets do it the "simple" way so you get a grasp of Javascript.
Hope all this helped you!!
Demo here:
http://jsfiddle.net/3GkrX/
Just about the same as Mevins.... changed to switch/case though
html:
<select id="test1" id="name" onchange="checkField()">
<option>----</option>
<option>Selected A Group</option>
<option>Selected B Group</option>
</select>
<div id="optional">Please select!</div>
JS:
function checkField(){
var temp = document.getElementById('test1').value;
switch(temp){
case "Selected A Group":
document.getElementById("optional").innerHTML="<select name='optionalA'><option>1</option><option>2</option></select>";
break;
case "Selected B Group":
document.getElementById("optional").innerHTML="<select name='optionalB'><option>3</option><option>4</option></select>";
break
default:
document.getElementById("optional").innerHTML="Please select!";
break;
}
}
Also added the second group as a real option, and a default as "please select". may or may not be necessary in your case
Here is the demo http://codepen.io/anon/pen/izAHo
your doing it almost right.
You should put the onclick event on the option tag to trigger changes based on the option selected.
HTML
<html>
<body>
<select id="test1">
<option onclick="checkField()">Selected A Group</option>
<option>Selected B Group</option>
</select>
<select id="test2">
<option onclick="check2Field()">Selected C Group</option>
<option>Selected D Group</option>
</select>
<script>//insert second group here</script>
</body>
</html>
JS
function checkField(){
var temp = document.getElementById('test1').value;
if(temp === "Selected A Group"){
document.getElementById('test2').innerHTML="<option>Selected halloooo Group</option>";
} else {
//insert code to "echo" the second optional select group
}
}
Check out my demo for more clarity.

jquery, get value of different select list

I have a lot of select list (hundreds) like this (they have all the same name and id (I think my problem comes from here... but I can't change it):
<select name="custom_element_grid_class" id="custom_element_grid_class" class="select-size">
<option value="normal">normal</option>
<option value="square">square</option>
<option value="wide">wide</option>
<option value="tall">tall</option>
</select>
I want to get value of each list when an user change the value. I made this script but it only works on my fist select list...
jQuery("#custom_element_grid_class").change(function(){
var element = jQuery(this);
var selected = element.find('option:selected');
var size = selected.val();
var sclass = size + " element isotope-item";
jQuery(element).closest('.element').attr('class',sclass);
});
How can I make it works for all my select form?
EDIT: each select list comes from an ajx call, that's the reason I've got the same ID, but only in the futur DOM.
You cannot have double ID's.
So my suggestion in calling a function by inline onchange in the select.
For example:
<select name="custom_element_grid_class" id="custom_element_grid_class" onchange="func(this)" class="select-size">
And then your function:
function func(el){
var element = el;
var size = element.value;
var sclass = size + " element isotope-item";
jQuery(element).closest('.element').attr('class', sclass);
};
Demo here
I would suggest adding a first option with no value, so that, as you say "when an user change the value" you can read in case he took the first value.
If you can help it avoid duplicate IDs in exchange for classes. If this isn't an option use an attribute selector. Modifying the above code slightly.
Document Ready
$(function()
{
$('[name=custom_element_grid_class]').change(function(){
var $element = $(this);
var size = $element.val();
var sclass = size + " element isotope-item";
$element.closest('.element').attr('class',sclass);
});
});
Fiddle

Categories

Resources