1

index.html

<html>
    <head>
        <script src="//code.jquery.com/jquery-2.0.0.min.js"></script>
        <script src="jquery.js"></script>
    </head>
    <body>
        <div id="myDiv" name="myDiv" title="Example Div Element">


        </div>
    </body>
</html>

jquery.js

$.ajax
({
 type: "GET",
 url: "url",
 dataType: 'image/png',
 async: false,

 beforeSend: function (xhr) {
 xhr.withCredentials = true;
 xhr.setRequestHeader("Authorization", "Bearer xxx");
 },
 complete: function (data) {
 console.log("yello");
 $('#myDiv').html('<img id="target">');
 }
 });

Simple enough question,

Why is this line not working? $('#myDiv').html('<img id="target">'); When I examine the source of the page nothing shows up, no image tag. I'm not quite understanding why as I am sure I am doing everything correctly.

Thanks!

7
  • Try $('#myDiv').html('Works !'); and see if text appears or not. Commented Jun 30, 2014 at 22:36
  • 4
    Where is your <body> tag? Commented Jun 30, 2014 at 22:37
  • Are you sure your ajax call is completing and your success function is being hit? Commented Jun 30, 2014 at 22:38
  • 1
    Are you using an inspector? If not the source will not reflect dynamic changes. Commented Jun 30, 2014 at 22:38
  • 1
    I am using the chrome inspector. Function should be hit whether or not the get completes (which it does). Body tag is there, im just stupid and ill paste it in now. .html('Works!) does not show up. Commented Jun 30, 2014 at 22:39

3 Answers 3

2

Wrap your function inside a .ready():

 $(function()
 {
     $.ajax
     ({
         type: "GET",
         url: "url",
         dataType: 'image/png',
         async: false,

         beforeSend: function (xhr) {
             xhr.withCredentials = true;
             xhr.setRequestHeader("Authorization", "Bearer xxx");
         },
         complete: function (data) {
             console.log("yello");
             $('#myDiv').html('<img id="target">');
         }
     });
 });

This makes sure to call the ajax method after the DOM is loaded.

Sign up to request clarification or add additional context in comments.

1 Comment

Answered as i commented, it worked! Thanks! Will check as soon as possible.
0

If the javascript you posted is the complete jquery.js file then what is happening is the javascript is being run before the browser has rendered the div tag so the jquery selector doesn't find anything, hence your console.log call returning undefined.

Here is a quick fiddle I through together to verify your javascript, note that I wrapped the image creation in a $(function(){}); block. this is jquery shorthand for document.ready(function(){});

Comments

0

try console.log($('#myDiv').html()) after your code and see the value in your console, you will find the image there but it wont show in source view

2 Comments

i demonestrated this locally dropbox.com/s/n93oocn8e2e2wxk/…
<html> <head> <script src="//code.jquery.com/jquery-2.0.0.min.js"></script> </head> <body> <div id="myDiv" name="myDiv" title="Example Div Element"> </div> </body> <script> $.ajax ({ type: "GET", url: "/images/cone.png", dataType: 'image/png', async: false, beforeSend: function (xhr) { xhr.withCredentials = true; xhr.setRequestHeader("Authorization", "Bearer xxx"); }, complete: function (data) { console.log("yello"); $('#myDiv').html('<img id="target">'); console.log($('#myDiv').html()); } }); </script> </html>

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.