0

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:ravi@gmail.com Dish: Indian Dishes

I want output like

UserName: Ravi  
ID: 123  
Email:ravi@gmail.com  
Dish: Indian Dishes
AVM
  • 592
  • 2
  • 11
  • 25
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
  • 1
    `detailsDiv.html("UserName: "+userName+"
    ID: "+ID+"
    Email:"+Email+"
    Dish: "+Dish);`
    – Bhojendra Rauniyar Jan 02 '15 at 12:49
  • possible duplicate of [How to force a line break on a Javascript concatenated string?](http://stackoverflow.com/questions/15357846/how-to-force-a-line-break-on-a-javascript-concatenated-string) – emerson.marini Jan 02 '15 at 12:50
  • 1
    Did you really need to show all that Ajax code just to ask about displaying newlines? – nnnnnn Jan 02 '15 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 Jan 02 '15 at 12:55

3 Answers3

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: //...
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
2

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>
ngasull
  • 4,206
  • 1
  • 22
  • 36
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
1

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>');
antyrat
  • 27,479
  • 9
  • 75
  • 76