aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Preserving a line break Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Preserving a line break" Watch "Preserving a line break" New topic
Author

Preserving a line break

Jan Caspers
Greenhorn

Joined: Jul 08, 2013
Posts: 4
Dear JavaRanchers, I am new to this Forum and to all things Java and hope that I am not imposing. I am encountering a problem with line breaks in a Wordpress feedback form. To give you the full details: Librarians are given a form to post activities. In order to make up the resulting posts' title from the library name and the activity date, I combine two fields from the form into one field in the uploaded post (following instructions - I did not and could not achieve this myself, yet...). It works great. This is the code:

<script type="text/javascript">
jQuery(document).ready(function($){
$('select[name="item_meta[208]"], select[name="item_meta[209]"]').change(function(){
var val1 = $("select[name='item_meta[208]']").val();
var val2 = $("select[name='item_meta[209]']").val();
{$("#field_x5476m").val(val1+' '+val2);}
});
});
</script>

Now I want to put a line break between val1 and val2, the library name and the date. But whatever I enter, it is stripped out. I tried first \n, which several people suggested, and also <br />, <br>, which I know from HTML but which may be completely wrong here. I searched high and low but could not find an answer - probably because I don't really know what it is I am searching for. The authors of the Wordpress form plugin could not think of an answer either. As I said, I am not sure if I am entitled to ask you, if I am in the right place here. But if you have any idea what might be amiss, and what might be the solution or even in which direction I should turn in order to find the solution, I would be very grateful indeed. Thank you for your patience, and for running the Ranch in the first place! Yours faithfully, Jan.
Jan Caspers
Greenhorn

Joined: Jul 08, 2013
Posts: 4
P.S.: Looking at my post, I probably left out an important detail. On line 6 I marked the place I am trying to insert the line break:

<script type="text/javascript">
jQuery(document).ready(function($){
$('select[name="item_meta[208]"], select[name="item_meta[209]"]').change(function(){
var val1 = $("select[name='item_meta[208]']").val();
var val2 = $("select[name='item_meta[209]']").val();
{$("#field_x5476m").val(val1+' HERE I AM TRYING TO INSERT THE LINE BREAK '+val2);}
});
});
</script>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

What is the element with id field_x5476m? If it's not a textarea, then line breaks are not supported in any other control.

If it is, the code for a line break is \n.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Welcome to the Ranch.

Well for javascript, line breaking is using "\n". Now given your code, if you already tried

and didn't work try this:

or


Not sure if the single quote and double quote makes a difference.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

K. Tsang wrote:Not sure if the single quote and double quote makes a difference.

No difference.
Jan Caspers
Greenhorn

Joined: Jul 08, 2013
Posts: 4
Dear Ranchers, dear K. Tsang, dear Bear Bibeault, my line was cut (internet line that is, not the text line I am indeed trying to cut in two!). Now, not an hour later, I find strangers extending a helping hand. Maybe this is normal and unremarkable - I am almost moved to tears, if you excuse me! I will try your suggestions tonight and learn how to properly post code! Plain \n did not work: I tried {$("#field_x5476m").val(val1+"\n"+val2);} and it is just read as a simple text n, nothing happens. {$("#field_x5476m") is the field that gets populated with the aggregate of val1 and val2. Excuse me if these quotes from the code are still wrong. I will go back to school now and read UseCodeTags, then try the suggestions. In the meantime I send you my sincere gratitude! Yours faithfully, Jan.
Jan Caspers
Greenhorn

Joined: Jul 08, 2013
Posts: 4
Dear Ranchers,

I have not quite understood the CodeTags or indentation, but your solution worked! Now the aggregate field is populated from the two original fields with a beautiful line break.

var val1 = $("select[name='item_meta[208]']").val();
var val2 = $("select[name='item_meta[209]']").val();
var val3 = val1+"\n"+val2;
{$("#field_x5476m").val(val3);}

My original problem is not solved yet, but at least I know what the problem is now: When I enter the aggregate field in question into the post, all is well. When I use it as a post title, the line break is still stripped out, but I learned that this is a Wordpress issue and can thus investigate in that direction. Apparently, Wordpress does not allow line breaks in post titles. When the title is long, it goes onto a second line alright but there seems to be no documented way to force the line break. Not a bug but a challenge, I suppose! In the meantime, I thank you from my heart that you helped me on my way towards my first steps in javascript. From your suggestions, I understood a principle. Worth much more than just a workable solution! Yours faithfully, Jan.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Nice to hear that.
 
Don't get me started about those stupid light bulbs.
 
subject: Preserving a line break