Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set initial value of a variable?

Status
Not open for further replies.

cbsarge

IS-IT--Management
Jun 20, 2001
219
US
I have this script and I would like to set the initial value of a variable that the script can later set with a drop down but, can;t figure it out. I'm sure it will take one if you a few seconds! :)

Code:
javascript:(function()
    {
        var previousInnerHTML = new String();
		window.onload = function() {
        previousInnerHTML = document.getElementById('goal_header_outer_wrapper').innerHTML;
		previousInnerHTML = previousInnerHTML.concat("<div align=\"center\"><br>");
		previousInnerHTML = previousInnerHTML.concat("<form style=\"display: inline\">");
		previousInnerHTML = previousInnerHTML.concat("<SELECT name=\"numberconsumables\" onchange=\"ConsumableNUM = this.options[selectedIndex].value; alert ('You will be using ' + ConsumableNUM + ' consumables when you click the Snack Bar butons')\">");
		previousInnerHTML = previousInnerHTML.concat("<OPTION selected value=\"1\">1</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"2\">2</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"3\">3</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"4\">4</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"5\">5</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"6\">6</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"7\">7</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"8\">8</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"9\">9</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"10\">10</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"11\">11</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"12\">12</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"13\">13</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"14\">14</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("<OPTION value=\"15\">15</OPTION>");
		previousInnerHTML = previousInnerHTML.concat("</SELECT>");
		previousInnerHTML = previousInnerHTML.concat("</form>");
        previousInnerHTML = previousInnerHTML.concat("<form style=\"display: inline\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Energy Drink\" onclick=\"ACTIONS.use_consumable(1,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Energy Infusion\" onclick=\"ACTIONS.use_consumable(5,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Atomic Energizer\" onclick=\"ACTIONS.use_consumable(9,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Nucleic Reactor\" onclick=\"ACTIONS.use_consumable(11,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Red Line\" onclick=\"ACTIONS.use_consumable(234,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Adrenaline Pill\" onclick=\"ACTIONS.use_consumable(13,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Adrenaline Injector\" onclick=\"ACTIONS.use_consumable(15,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("<input type=\"button\" class=\"btnFJ\" value=\"Stamina Serum\" onclick=\"ACTIONS.use_consumable(236,ConsumableNUM)\">");
		previousInnerHTML = previousInnerHTML.concat("</form>");
		previousInnerHTML = previousInnerHTML.concat("</div>");
        document.getElementById('goal_header_outer_wrapper').innerHTML = previousInnerHTML;
		};
    }
)();
 
Hi

First let us make it readable :
JavaScript:
window[teal].[/teal]onload [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'goal_header_outer_wrapper'[/i][/green][teal]).[/teal]innerHTML[teal]+=[/teal]
     [green][i]'<div align="center"><br>'[/i][/green]
    [teal]+[/teal][green][i]'<form style="display: inline">'[/i][/green]
    [teal]+[/teal][green][i]'<SELECT name="numberconsumables" onchange="ConsumableNUM = this.options[selectedIndex].value; alert ([/i][/green][lime][i]\'[/i][/lime][green][i]You will be using [/i][/green][lime][i]\'[/i][/lime][green][i] + ConsumableNUM + [/i][/green][lime][i]\'[/i][/lime][green][i] consumables when you click the Snack Bar butons[/i][/green][lime][i]\'[/i][/lime][green][i])">'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION selected value="1">1</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="2">2</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="3">3</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="4">4</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="5">5</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="6">6</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="7">7</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="8">8</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="9">9</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="10">10</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="11">11</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="12">12</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="13">13</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="14">14</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION value="15">15</OPTION>'[/i][/green]
    [teal]+[/teal][green][i]'</SELECT>'[/i][/green]
    [teal]+[/teal][green][i]'</form>'[/i][/green]
    [teal]+[/teal][green][i]'<form style="display: inline">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Energy Drink" onclick="ACTIONS.use_consumable(1,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Energy Infusion" onclick="ACTIONS.use_consumable(5,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Atomic Energizer" onclick="ACTIONS.use_consumable(9,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Nucleic Reactor" onclick="ACTIONS.use_consumable(11,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Red Line" onclick="ACTIONS.use_consumable(234,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Adrenaline Pill" onclick="ACTIONS.use_consumable(13,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Adrenaline Injector" onclick="ACTIONS.use_consumable(15,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'<input type="button" class="btnFJ" value="Stamina Serum" onclick="ACTIONS.use_consumable(236,ConsumableNUM)">'[/i][/green]
    [teal]+[/teal][green][i]'</form>'[/i][/green]
    [teal]+[/teal][green][i]'</div>'[/i][/green]
[teal]}[/teal]
And now the question : when and while should that default value appear ?

Feherke.
 
Just when the page first displays so if someone wanted to just leave it on the default selection of 1 the onclick event would still work. For now I have added another selection with a null value and a note telling the users they need to select something but, it would be better if it worked without having to select 1 first.
 
Hi

Then just call the [tt]select[/tt]'s [tt]onchange[/tt] manually :
JavaScript:
window[teal].[/teal]onload [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'goal_header_outer_wrapper'[/i][/green][teal]).[/teal]innerHTML[teal]+=[/teal]
     [green][i]'<div align="center"><br>'[/i][/green]
    [teal]+[/teal][green][i]'<form style="display: inline">'[/i][/green]
    [teal]+[/teal][green][i]'<SELECT name="numberconsumables" [highlight]id="whatever"[/highlight] onchange="ConsumableNUM = this.options[selectedIndex].value; alert ([/i][/green][lime][i]\'[/i][/lime][green][i]You will be using [/i][/green][lime][i]\'[/i][/lime][green][i] + ConsumableNUM + [/i][/green][lime][i]\'[/i][/lime][green][i] consumables when you click the Snack Bar butons[/i][/green][lime][i]\'[/i][/lime][green][i])">'[/i][/green]
    [teal]+[/teal][green][i]'<OPTION selected value="1">1</OPTION>'[/i][/green]
[gray]// ... all that code...[/gray]
    [teal]+[/teal][green][i]'</form>'[/i][/green]
    [teal]+[/teal][green][i]'</div>'[/i][/green]
  [highlight]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'whatever'[/i][/green][teal]).[/teal][COLOR=darkgoldenrod]onchange[/color][teal]()[/teal][/highlight]
[teal]}[/teal]
Note that I added an [tt]id[/tt] to the [tt]select[/tt] for easier reference, but you can also reference it by [tt]name[/tt] if you prefer that.


Feherke.
 
That worked great Feherke - thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top