Zeca Novaes
Zeca Novaes

Reputation: 419

Inject script inside ajax loaded div

I'm using Jquery in my site and I have a div with content loaded by ajax, like an iFrame. With my code I can access the content and even change the class and values, but I can't insert a script to be executed.

There is the code:

<script>
$("#loginsb").click(function () {

$('#curso')
          .contents()
          .find('#script').jQuery("<script>").prop("tagName")
          .attr('src', load.js);
});
</script>

My ajax loaded div is curso and inside this div I have another div script where I placed the the script line - without the src. Is the only way I knew to use more than one script in my page and find just one of them.

Inside Curso div

<div id="script">
<script></script>
</div>

Any idea to attribute this src inside my div script? Thanks!

Upvotes: 0

Views: 229

Answers (1)

Klaassiek
Klaassiek

Reputation: 2906

You miss some basics in understanding Javascript and jQuery. First thing: you can only have one element with id="script" and in your example you have one script element inside it.

So you can target that script element inside the script id element using jQuery:

$('#script script')

Then adjust the source of the script:

$('#script script').attr('src', 'load.js');

(Note the quotation marks around load.js. this is a string literal not a variable or constant.)

Upvotes: 1

Related Questions