Arturo Suarez
Arturo Suarez

Reputation: 665

Insert element into div with jQuery, append not working

I have the following code:

$('input[id$="txtTecnicas"]').bind('keypress', function (e) {
            //if user presses enter
            if (e.keyCode == 13) {
                //cancel asp.net postback
                e.preventDefault();

                //if value of textbox is not empty
                if ($(this).val() != "") {

                    //Save value into valueTec
                    var valueTec = $(this).val();
                    //Clear textbox value
                    $(this).val("");
                    //Create tag div to insert into div
                    var texthtml = '<span class="tag" title="' + valueTec + '">' + valueTec + ' <a>x</a><input type="hidden" value="1" name="tags"></span>';

                    //Append the new div inside tecTagHolder div
                    $('[id$="tecTagHolder"]').append(texthtml);
                }
            }
        });

However, it is not inserting the code into tecTagHolder div, and since it need to insert into tecTagHolder many divs, one each time user press enter, I cannot use .html() command but with append it doesn't append even a "Hello" string! what could it be wrong? Thanks a lot!

Upvotes: 1

Views: 914

Answers (2)

Maikel D
Maikel D

Reputation: 301

You can use something like this:

$('[id$="tecTagHolder"]').html( $('[id$="tecTagHolder"]').html() + texthtml );

But I'd recommend you generate that element like this:

var span = $('<span></span>').addClass('tag').attr('title', valueTec).text(valueTec);
var a = $('<a></a>').text('x');
.. and so on

Then construct the elements using like such:

span.append(a).append(input);

Then you can append the span into $('[id$="tecTagHolder"]').

Let me know if that helps :)

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

Why not just select the tecTag like so:

$("#<%=tecTagHolder.ClientID %>").append(texthtml);

Upvotes: 2

Related Questions