Satch3000
Satch3000

Reputation: 49422

JQuery On Select change assign value to variable and reload script

Is it possible at all to be able to assign the selected value of a select and assign it to the variable of a script and re-run the script?

Here is my current code sample:

<select onchange="reload_script;">
  <option value="123">Select 123</option>
  <option value="456">Select 456</option>
<select>

<div id="123">
    <a href="http://same.com/index.do?n=152991">link title</a>
    <br/><br/>
    <a href="http://some.com/">Link</a>
</div>
<script language="javascript" type="text/javascript">
var code = '123' // <-- THIS VARIABLE WOULD BE THE VALUE OF THE SELECTED DROPDOWN VALUE
</script>

See: var code = '123' .... That's what's changed when the select box is changed ... then I would need the script to re-run...

Is this possible at all?

Upvotes: 0

Views: 3553

Answers (2)

Imdad
Imdad

Reputation: 6042

Try this

<select onchange="reload_script();">
  <option value="123">Select 123</option>
  <option value="456">Select 456</option>
<select>

<div id="123">
    <a href="http://same.com/index.do?n=152991">link title</a>
    <br/><br/>
    <a href="http://some.com/">Link</a>
</div>
<script language="javascript" type="text/javascript">
var code = '123' // <-- THIS VARIABLE WOULD BE THE VALUE OF THE SELECTED DROPDOWN VALUE
function script_to_run()
{
//do code here
}

// Call the script one time on page load (only if you need it on page load)
script_to_run();

function reload_script()
{
  script_to_run();//run again
}


</script>

Upvotes: 1

VisioN
VisioN

Reputation: 145458

If you use jQuery you can always bind change event:

var code = "123";
$("#mySelect").on("change", function() {
    code = this.value;
    reload_script();
});

Otherwise, in plain JavaScript it may look like this:

var code = "123";
document.getElementById("mySelect").onchange = function() {
    code = this.value;
    reload_script();
};

Upvotes: 3

Related Questions