Helio Bentzen
Helio Bentzen

Reputation: 677

Jquery to change src of a script tag using a select value

I need to change the value of the src in the tag :

<script type='text/javascript' src='gray.js'></script>

For this I'm using a combobox:

<select id="chartTheme">
   <option value="dark-blue">Dark Blue</option>
   <option value="gray">Gray</option>
</select>

How to make it use a Jquery function when I select and click in a refresh button?

Source to refresh button:

<input type="image" name="commit" src="../icons/refresh.png" onClick="javascript:updateGraph()"></input>

Upvotes: 1

Views: 4277

Answers (4)

karacas
karacas

Reputation: 2132

$('#chartTheme').change(function () {
    var optionVal = $(this).val();
    var miSrc = "someSrc";
    $('input.someClass').attr('src', miSrc);
    updateGraph();
});

Upvotes: 0

Jai
Jai

Reputation: 74738

I think this should be like this:

$('input[name="commit"]').click(function(){
    var srcExt = $('#chartTheme').val();
    $('<script'+' type="text/javascript"'+' src='+ srcExt +'></'+'script>').appendTo('body');
});

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173642

You could use $.getScript() for this purpose. It loads a new script into the page and runs the code inside.

function updateGraph()
{
    var sel = $('#chartTheme').val();

    $.getScript(sel + '.js');
}

Upvotes: 2

karacas
karacas

Reputation: 2132

Try this:

$('#chartTheme').change(function () {
    var optionVal = $(this).val();
    console.log(optionVal);
    updateGraph();
});

Upvotes: 0

Related Questions