1

i want to insert image on page using javascript function

//JS
$('#fff').html('<img src="../images/pic.jpeg">');

when i write the url as above ...nothing loaded.

How can i write correct URL ?

5
  • Use image_tag Commented Jul 8, 2012 at 14:59
  • can i write it inside js code ? And How ? Commented Jul 8, 2012 at 15:00
  • 1
    Oh I see. Nope you can't. You should have to generate the js using php or use absolute path for your image ie: /images/pic.jpg Commented Jul 8, 2012 at 15:01
  • For one, you didn't close your HTML tag, it should read: $('#fff').html('<img src="../images/pic.jpeg">'); Commented Jul 8, 2012 at 15:02
  • @PeanutButterJelly : it's not the issue here Commented Jul 8, 2012 at 15:04

1 Answer 1

2

If the js code is in your view template, then you do do

$('#fff').html('<?php echo image_tag('pic.jpeg') ?>');

else if it is in a separate js file, you need to use the absolute path for it.

$('#fff').html('<img src="/images/pic.jpeg">');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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