2

I'm having trouble with this code

  output.html('<img  width="200" src="uploads/'+response.responseText+'" height="300" >');

It outputs this:

   <img width="200" height="300" 1269050260.png""="" src="uploads/">

It should be:

   <img width="200" height="300"  src="uploads/1269050260.png">

I have tried many ways, for example:

 output.html('<img  width="200" src="uploads/'+response.responseText+'" height="300" >');
 output.html('<img  width="200" src="uploads/"'+response.responseText+'" height="300" >');
   .....

But when I try this:

  output.html("<img width='200' src='uploads/"+response.responseText+" height='300' >");

it gives

  <img width="200" 300'="" src="uploads/"1074374482.png" height=">

which also wrong.

EDIT

When I tried this suggested below:

   output.html("<img width='200' src='uploads/"+response.responseText+"' height='300' >");

I got this which is also wrong.

  <img width="200" height="300" src="uploads/"977165688.png"">

When I try:

    output.html("<img width='200' src='uploads/977165688.png' height='300' >"); // this works
5
  • I don't know if this will make a difference, but on your last example, it should be src='uploads/"+response.responseText+"' .You forgot to close the src. Commented Aug 25, 2014 at 23:17
  • I might try declaring a variable like var src = 'uploads/'+response.responseText and then in .html() use "src="+src Commented Aug 25, 2014 at 23:20
  • 3
    How certain are you of the contents of response.responseText? If I hardcode the value of response.responseText, then your first example works just fine... Commented Aug 25, 2014 at 23:20
  • @J148 added your suggested code to my question in edit. Commented Aug 25, 2014 at 23:21
  • @J148 have tried decalring that variable also but it same result. Commented Aug 25, 2014 at 23:21

2 Answers 2

3

It's better to just escape the " than to try and use quote rules like that. Try this:

output.html("<img  style=\" width: 200px; height: 300px; \" src=\"uploads/" + response.responseText + "\">");

Fiddle

If that doesn't work then you've got other problems.

EDIT:

Also the width and height attributes are deprecated for the <img> tag. Don't use them. Use CSS instead. Example has been updated

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

13 Comments

your output is this <img width="200" height="300" 708561310.png""="" src="uploads/">
That's what you see in the Fiddle I posted?
@your fiddle is just getting data from fiddel.shell.com but nothing shows .
That's a strange error, never seen that from jsfiddle. Try the new link.
what im suspected to see in your fiddle ?, i see just rectangular border.
|
1

jQuery html() accept a HTMLString as argument, you should try to parse it as HTML if you're not sure about response.responseText (and you should encode it since it will belong to a src attribute):

output.html($.parseHTML('<img  width="200" src="uploads/' + encodeURIComponent(response.responseText) + '" height="300" >'));

9 Comments

your outputs is this <img width="200" height="300" 166748152.png""="" src="uploads/">
Are you using jQuery OP?
@colepanike .html() is a jquery method
yes i have jquery , but this gives me $.parseHTML is not a function
What version of jQuery are you using?
|

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.