javascript - How to add new line in jQuery function text() - Stack Overflow

I want to insert new line in jQuery function.$.ajax({ url:"http: 192.168.1.4Experiementswebser

I want to insert new line in jQuery function.

$.ajax({ 
        url:"http: //192.168.1.4/Experiements/webservices/api.php", 
        type:"GET", 
        dataType:"json", 
        data:{type:"login", UserName:userId,Password:userPassword}, 
        ContentType:"application/json", 
        success: function(response){                        
        alert(JSON.stringify(response));                                   
              var detailsDiv=$("div#details");
              var userName=response[0].User.UserName;                              
              var ID=response[0].User.Followers;
              var Email=response[0].User.email;
              var Dish=response[0].User.Dish;                              
             detailsDiv.text("UserName: "+userName+"ID: "+ID+"Email: "+Email+"Dish: "+Dish);
         }, 

Check the following line in above code: detailsDiv.text("UserName: "+userName+"ID: "+ID+"Email:"+Email+"Dish: "+Dish); It is giving output like: UserName: Ravi ID: 123 Email:[email protected] Dish: Indian Dishes

I want output like

UserName: Ravi  
ID: 123  
Email:[email protected]  
Dish: Indian Dishes

I want to insert new line in jQuery function.

$.ajax({ 
        url:"http: //192.168.1.4/Experiements/webservices/api.php", 
        type:"GET", 
        dataType:"json", 
        data:{type:"login", UserName:userId,Password:userPassword}, 
        ContentType:"application/json", 
        success: function(response){                        
        alert(JSON.stringify(response));                                   
              var detailsDiv=$("div#details");
              var userName=response[0].User.UserName;                              
              var ID=response[0].User.Followers;
              var Email=response[0].User.email;
              var Dish=response[0].User.Dish;                              
             detailsDiv.text("UserName: "+userName+"ID: "+ID+"Email: "+Email+"Dish: "+Dish);
         }, 

Check the following line in above code: detailsDiv.text("UserName: "+userName+"ID: "+ID+"Email:"+Email+"Dish: "+Dish); It is giving output like: UserName: Ravi ID: 123 Email:[email protected] Dish: Indian Dishes

I want output like

UserName: Ravi  
ID: 123  
Email:[email protected]  
Dish: Indian Dishes
Share Improve this question edited Jan 2, 2015 at 13:07 AVM 5922 gold badges11 silver badges26 bronze badges asked Jan 2, 2015 at 12:48 Neelabh SinghNeelabh Singh 2,67812 gold badges55 silver badges91 bronze badges 4
  • 1 detailsDiv.html("UserName: "+userName+"<br />ID: "+ID+"<br />Email:"+Email+"<br />Dish: "+Dish); – Bhojendra Rauniyar Commented Jan 2, 2015 at 12:49
  • possible duplicate of How to force a line break on a Javascript concatenated string? – emerson.marini Commented Jan 2, 2015 at 12:50
  • 1 Did you really need to show all that Ajax code just to ask about displaying newlines? – nnnnnn Commented Jan 2, 2015 at 12:52
  • 2 This is clearly not a duplicate of the linked question, but shows a misunderstanding of browsers' rendering in a similar vain. – JAAulde Commented Jan 2, 2015 at 12:55
Add a ment  | 

3 Answers 3

Reset to default 3

Change .text to .html and use <br> element, it inserts a line break:

detailsDiv.html("UserName: "+userName+"<br>ID: "+ID+"<br>Email: "+Email+"<br>Dish: "+Dish);
//                                      ^^            ^^                  ^^

If you are writing XHTML, then the <br> tag must be closed, like this <br />:

detailsDiv.html("UserName: "+userName+"<br />ID: //...

Try this...

jQuery:

$.ajax({ 
        url:"http: //192.168.1.4/Experiements/webservices/api.php", 
        type:"GET", 
        dataType:"json", 
        data:{type:"login", UserName:userId,Password:userPassword}, 
        ContentType:"application/json", 
        success: function(response){                        
        alert(JSON.stringify(response));                                   
              var detailsDiv=$("div#details");
              var userName=response[0].User.UserName;                              
              var ID=response[0].User.Followers;
              var Email=response[0].User.email;
              var Dish=response[0].User.Dish;                              
            $("#username").html(userName);
            $("#userid").html(ID);
            $("#email").html(Email);
            $("#dish").html(Dish);
         },
});

HTML:

         <p id="username"></p>
         <p id="userid"></p>
         <p id="email"></p>
         <p id="dish"></p>

You can use p elements for example and append method:

detailsDiv.append('<p>UserName: ' + userName + '</p>');
detailsDiv.append('<p>ID: ' + ID + '</p>');
detailsDiv.append('<p>Email: ' + Email + '</p>');
detailsDiv.append('<p>Dish: ' + Dish + '</p>');

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信