javascript - Set image in an input value - Stack Overflow

I am trying to include an img tag inside an input's value attribute. I would like something like t

I am trying to include an img tag inside an input's value attribute. I would like something like this using jquery:

<input type="text" value="Lorem Impsum <img src='...'/>" />

I already tried this:

jQuery(document).ready(function() { 
     productinput = jQuery('input[value=product]');
     productinput.val(data.selectedData.value + '<img style="width:128px" src="' + data.selectedData.imageSrc + '" />');
});     

but it's not working. So, does anyone know how I can do this?

Note: it is an image inside the value.

I am trying to include an img tag inside an input's value attribute. I would like something like this using jquery:

<input type="text" value="Lorem Impsum <img src='...'/>" />

I already tried this:

jQuery(document).ready(function() { 
     productinput = jQuery('input[value=product]');
     productinput.val(data.selectedData.value + '<img style="width:128px" src="' + data.selectedData.imageSrc + '" />');
});     

but it's not working. So, does anyone know how I can do this?

Note: it is an image inside the value.

Share Improve this question edited Jan 27, 2016 at 14:58 wpercy 10.1k4 gold badges34 silver badges49 bronze badges asked Jan 27, 2016 at 11:00 KelyaneKelyane 4672 gold badges7 silver badges19 bronze badges 3
  • why do you need to use image like this? – Parth Trivedi Commented Jan 27, 2016 at 11:02
  • I would say that's not valid HTML? – mnille Commented Jan 27, 2016 at 11:03
  • The img tag would not be parsed, it's a string in this context. So unless you want to send this input's value to server and save it somewhere, you can't do this – Adam Azad Commented Jan 27, 2016 at 11:05
Add a ment  | 

3 Answers 3

Reset to default 1

You can set the image by putting image as the background of the element

For example

html

<input type="text" value="Lorem Impsum" class="default" />

css

input.default{
       background: url('../images/any.jpg') 0 0 no-repeat;
    }

You can set the css of the element using jquery on document ready.

You should set the type to image.

<input type="image" name="image" src="image.png" width="50">

Try to read doc: Mozilla documentation input/image

Try using the attribute "contenteditable" on a div

<div contenteditable> Lorem ipsum.. <img src='..'> </div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745238346a4618025.html

相关推荐

  • javascript - Set image in an input value - Stack Overflow

    I am trying to include an img tag inside an input's value attribute. I would like something like t

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信