0

I have tried a lot to add line break but it dose not work... replace \n with line brack I'm going to save html form in txt format in Notepad see all input text with one line which is i don't want that...

need your help please.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript">
<!--


function buildData(){
var txtData = "00"+$("#nameField").val()+
        "  "+$("#title").val()+
        "0000000000"+$("#lastNameField").val()+
        "\r\n Gender: "+($("#genderMale").is(":checked")?"Male":"Female");

return txtData;
}
// This will be executed when the document is ready
$(function(){
// This will act when the submit BUTTON is clicked
$("#formToSave").submit(function(event){
    event.preventDefault();
    var txtData = buildData();
    window.location.href="data:application/octet-stream;base64,"+Base64.encode(txtData);
});

// This will act when the submit LINK is clicked
$("#submitLink").click(function(event){
    var txtData = buildData();
    $(this).attr('download','sugguestedName.txt')
        .attr('href',"data:application/octet-stream;base64,"+Base64.encode(txtData));
});
});
//-->
</script>
</head>
<body>
<form method="post" action="" id="formToSave">
<dl>
    <dt>Date:</dt>
    <dd>
    <input type="text" id="nameField" value="00002014" />
    <input type="text" id="title" value="Credit Card Payment" />
    </dd>
    <dt>Card No:</dt>
    <dd><input type="text" id="lastNameField" value="Last Name" /></dd>
    <dt>Gender:</dt>
    <dd><input type="radio" checked="checked" name="gender" value="M" id="genderMale" />
        Male
        <input type="radio" checked="checked" name="gender" value="F" />
        Female
</dl>
<p><a href="javascript://Save as TXT" id="submitLink">Save as TXT</a></p>
<p><button type="submit"><img src="http://www.suttonrunners.org/images/save_icon.gif" alt=""/> Save as TXT</button></p>

thank in advance...

user1710911
  • 657
  • 3
  • 7
  • 15

2 Answers2

0

I think your problem is not in Javascript, but in the way it is shown afterwards. Try alert(buildData()), does it work properly? If you're appending the text to the DOM, you have to use HTML line feeds, like <br>.

Mathias
  • 1,470
  • 10
  • 20
0

Tried running the above mentioned after removing the jQuery added in. It did work as expected.

  1. Please make sure that the returned string "txtData" is properly concatenated.
  2. If properly concatenated then it should work unless being appended to DOM in which case you have to use a
    tag