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

7
  • what are you trying to achieve ? Commented Feb 12, 2016 at 5:46
  • seriously?? well its funny because i can't even imagine to do that. sorry! but its not possible Commented Feb 12, 2016 at 5:48
  • Possible duplicate stackoverflow.com/questions/19236778/… Commented Feb 12, 2016 at 5:51
  • stackoverflow.com/questions/17938461/… Commented Feb 12, 2016 at 5:52
  • 1
    Shivam Tyagi's answer solved my issue.Thank you Commented Feb 12, 2016 at 10:10

5 Answers 5

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

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

Comments

1

Putting div inside input is not possible .

Check Here

Comments

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">

1 Comment

it will return the object so object says it self that is added inside
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">

Comments

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);

5 Comments

is this same funtion I put above?
PLease Compare and Check @Buddhika Kulathilaka
@javedrathod It will add div inside input?
yes it will put on click of input , and next time please check code and put your review
@javedrathod OP doesn't want to display div element inside input. OP want to add div. Please read question first. jsfiddle.net/8sa6n2o3

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.