0
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#usr").click(function () {
                var divElement = "<div style='color:yellow;'>more text</div>";
                $('#usr').val($('#usr').val() + divElement);
            });
        });
    </script>
</head>
<body>
    <input class="form-control" id="usr">
</body>
</html>

I want to add this div element inside the text box but it get as the string and display.

Is it possible to do this or is there any other way to do this like input. Thank you

John R
  • 2,741
  • 2
  • 13
  • 32

5 Answers5

1

Putting div inside input is not possible .

Check Here

brk
  • 48,835
  • 10
  • 56
  • 78
1

It is not possible to add div inside an input box but you can use contenteditable div instead of input field, if you have no such issue in using that.

And you can add anything inside that.

http://www.w3schools.com/tags/att_global_contenteditable.asp

Shivam Tyagi
  • 158
  • 1
  • 12
0

it will add in input and return object type as output

<!DOCTYPE html>
 <html>
 <head>
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">     </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#usr").click(function(){

    $('#usr').val($(this).html('<div></div>'));
});
});
</script>
</head>
<body>
<input class="form-control" id="usr">
abhay vyas
  • 136
  • 2
0
$(document).ready(function(){
$("#usr").click(function(){
var divElement="<div style='color:yellow;'>more text</div>";
    $('#usr').val($('#usr').val() + divElement);
});
});

and when you want to add any html element then use append like below:

$('#usr').append($('#usr').val() + divElement);
JavidRathod
  • 483
  • 2
  • 10
0

You can not put div Or any other element inside input. You can insert simple text inside input.

like:

$(document).ready(function(){
$("#usr").click(function(){
var divElement="more text";
    $('#usr').val($('#usr').val() + divElement);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input class="form-control" id="usr">
ketan
  • 19,129
  • 42
  • 60
  • 98