1

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>
3
  • 1
    Why do you want to change src? Just load a new script by getScript? Commented Nov 23, 2012 at 16:14
  • why do you want to load two different script files? It would be better to use a single file for you code and make your calls read a variable which you manipulate when you change the selection ... Commented Nov 23, 2012 at 16:16
  • The original script will already be loaded. No point in changing .src. may as well just load a new script or call a different function in the same script. Commented Nov 23, 2012 at 16:24

4 Answers 4

2

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

But, i have another scripts, like <script src="/js/jquery-1.7.2.js" type="text/javascript"></script> <script src="/js/charts.js" type="text/javascript"></script>
@HelioBentzen I don't see how that affects anything?
Forget it. it's working.. i just need use a timeout to load my graph because I called other function to render. Thanks a lot, Jack!
0

Try this:

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

1 Comment

-1. "I need to change the value of the src in the tag" - I don't see how that's addressed here.
0

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');
});

1 Comment

When I try it, the java make a wrong intrepretation of the <script'
0
$('#chartTheme').change(function () {
    var optionVal = $(this).val();
    var miSrc = "someSrc";
    $('input.someClass').attr('src', miSrc);
    updateGraph();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.