Denisson de Souza
Denisson de Souza

Reputation: 65

How to get rid of html tags when output data

I am having a issue with the syntax. How to get rid of html tags in jQuery?

$.ajax({
        type: "POST",
        url: '/populate',
        data: {id: id, '_token':$('input[name=_token]').val()},
        success: function( data ) {
          var results = [
              data.address,
              data.state,
              data.city,
              data.country,
              data.code
          ];

          $("#title").val(data.title);
          $("#address").val(results.map(function(value) {
               return(value + "<br>";
            }).join(""));

        }
    });

When displaying results it shows html tag. how to fix it please?

9111 Parisian Mountain
Cletamouth, MO 99096-0914<br>Pennsylvania<br>West 
Khalilborough<br>Uzbekistan<br>23457-3306

Upvotes: 0

Views: 62

Answers (2)

3gth
3gth

Reputation: 550

i think you are displaying result inside a textarea if so use \n instead of <br>

<br> will work if you are trying to insert line break in html code but as address is an input element for new line characters to work you should use \n

var results = ["9111 Parisian Mountain Cletamouth, MO 99096-0914","Pennsylvania","West Khalilborough","Uzbekistan","23457-3306"]; 

$("#address").val(results.map(function(value) {
       return(value + "\n");
  }).join(""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="address" cols="55" rows="5"></textarea>

Upvotes: 1

Tom Headifen
Tom Headifen

Reputation: 1996

Use .html() instead of .val() for HTML

  $("#address").html(results.map(function(value) {
       return(value + "<br>");
  }).join(""));

http://api.jquery.com/html/#html-htmlString

Upvotes: 0

Related Questions