1

i am a jquery learner ,

My table

<table id="table">
<tr >
    <th>column 1</th>
    <th>Address</th>
    <th>column 3</th>
</tr>
<tbody>
    <tr>
        <td>test data</td>
        <td class="address">My compnay, \n New Hil \n
            six singama \n India
        </td>
        <td>some data</td>
    </tr>
     <tr>
        <td>test data</td>
         <td class="address">New Branch, \n New Hil \n
            six singama \n India
        </td>
        <td>some data</td>
    </tr>
     <tr>
        <td>test data</td>
         <td class="address">Some company, \n New arcade \n
            six singama \n India
        </td>
        <td>some data</td>
    </tr>
   </tbody> 

Second column of each row has a address which contain \n character in it, i want to replace \n with <br> tag on loading the page (when document is ready)

Jsfiddle here

 $( document ).ready(function() {
   var columns = $('tr').find('td:eq(2  )')
  });
3
  • Why can't you just get rid of the \n in the source? Commented Nov 22, 2013 at 17:40
  • i am getting data in that format from remote server Commented Nov 22, 2013 at 17:46
  • Check this link : stackoverflow.com/questions/7467840/… Commented Nov 22, 2013 at 17:53

1 Answer 1

3

Try:

$(document).ready(function() {
    $("tr:gt(0)").each(function(){
        var t = $(this).find("td:eq(1)");
        var tx = $(t).text();
        tx = tx.replace(/\\n/g,'<br/>');
        $(t).html(tx);
    });
});

Fiddle here.

You can also try like this:

$(document).ready(function() {
    $("td:contains(\n)").each(function(){        
        var t = $(this).text();        
        t = t.replace(/\\n/g,'<br/>');
        $(this).html(t);
    });
});

Fiddle here.

Sign up to request clarification or add additional context in comments.

1 Comment

Everyobody else removed their answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.