简体   繁体   中英

How to append a text to div from jquery?

I have append it using jquery but it will never display in dropdown list

for (var key in data)
 {
    //alert(data[key]["type"] + data[key]["id"] + data[key]["message"]);
    //$str = $str+;
    $(".notiDiv").append('<div class="row" style="margin: 1px">\n\<div class="col - sm - 8">\n\<a href="#">' user have created a meeting'</a>\n\</div>\n\<div class="form - group col - sm - 4 text - right"  >\n\<a><span class=" glyphicon glyphicon - ok"></span></a>\n\<a><span class=" glyphicon glyphicon - remove"></span></a>\n\</div>\n\</div>\n');
 }
<div class="dropdown-menu dropdown-menu-custom " >
    <div class="notiDiv">

    </div>
</div></li>

You have used some unexpected blank spaces in class names and \\n , you should remove those

Your code will be like

    $(".notiDiv").append('<div class="row" style="margin: 1px"><div class="col-sm-8"><a href="#!">user have created a meeting</a></div><div class="form-group col-sm-4 text-right"  ><a href="#!"><span class=" glyphicon glyphicon-ok"></span></a><a href="#!" ><span class=" glyphicon glyphicon-remove"></span></a></div></div>');

Once you fix all JS errors if any as suggested by Satpal; you will need to initialize bootstrap dropdown programmatically as follows:

for (var key in data)
 {
    //alert(data[key]["type"] + data[key]["id"] + data[key]["message"]);
    //$str = $str+;
    $(".notiDiv").append('<div class="row" style="margin: 1px">\n\<div class="col - sm - 8">\n\<a href="#">' user have created a meeting'</a>\n\</div>\n\<div class="form - group col - sm - 4 text - right"  >\n\<a><span class=" glyphicon glyphicon - ok"></span></a>\n\<a><span class=" glyphicon glyphicon - remove"></span></a>\n\</div>\n\</div>\n');
 }
 $(".notiDiv").dropdown(); //re-init dropdowm once all options has been appended

You can check code here, i have created an example for you

http://plnkr.co/edit/seRGvP?p=preview

$( document ).ready(function() {
  var data = {
    "flammable": "inflammable",
    "duh": "no duh"
  };
  $.each(data, function( key, value ) {
    //alert( key + ": " + value );
    $(".notiDiv").append('<div class="row" style="margin: 1px">\n\<div class="col - sm - 8">\n\<a href="#">' + value + '</a>\n\</div>\n\<div class="form - group col - sm - 4 text - right"  >\n\<a><span class=" glyphicon glyphicon - ok"></span></a>\n\<a><span class=" glyphicon glyphicon - remove"></span></a>\n\</div>\n\</div>\n');
  });
});

or with array

$( document ).ready(function() {
   var data = ['aaaaaaaaaaaa','bbbbbbbbbbbbbb','ccccccccccccc','dddddddddddd','eeeeeeeeeeee','ffffffffffffff','ggggggggggg','jjjjjjjjjjjj'];
   $(data).each( function( index, value ) {
      //alert( index + ": " + value );
       //$( ".notiDiv" ).append( value );
       $(".notiDiv").append('<div class="row" style="margin: 1px">\n\<div class="col - sm - 8">\n\<a href="#">' + value + '</a>\n\</div>\n\<div class="form - group col - sm - 4 text - right"  >\n\<a><span class=" glyphicon glyphicon - ok"></span></a>\n\<a><span class=" glyphicon glyphicon - remove"></span></a>\n\</div>\n\</div>\n');
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM