GGw
GGw

Reputation: 443

JQuery does not read the value properly even with quote

So I have this value from my database column:

\u25cf10:00-10:50 IELTS Listening Randy  change to \r\n(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel\r\n\u25cf16:00-16:50 Ed IELTS  Reading \r\n(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose\r\n\r\nMy class schedule on the system is wrong~ please kindly check it, thank you very much!\r\n\r\n

This value is from column named Remark

So I will show the data using JQuery post data and get the values of each columns, Other columns were fine but when I get into the Remark it shows like this: enter image description here

How did this happen? I tried replace but still does not work for me

Upvotes: 2

Views: 1021

Answers (2)

Ntiyiso Rikhotso
Ntiyiso Rikhotso

Reputation: 720

The following code should work, I have used backticks instead of quotes

var data = `\u25cf10:00-10:50 IELTS Listening Randy  change to \r\n(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel\r\n\u25cf16:00-16:50 Ed IELTS  Reading \r\n(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose\r\n\r\nMy class schedule on the system is wrong~ please kindly check it, thank you very much!\r\n\r\n`

$('[name="form-remarks"]').val(data)
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="form-remarks"/>

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337666

The problem is caused by the line breaks - \r\n. The quick way to fix this is to use template literals as they can cope with line breaks:

$('[name="form-remarks"]').val(`●10:00-10:50 IELTS Listening Randy  change to 
(1) 09:00-09:50 Rose or (2)  11:00-11:50 Zel
●16:00-16:50 Ed IELTS  Reading 
(1) 15:00-15:50 Jane or (2) 13:00-13:50 Rose

My class schedule on the system is wrong~ please kindly check it, thank you very much!

`);
textarea {
  width: 600px;
  height: 120px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea name="form-remarks"></textarea>

Note that this will not work in IE, though.

Alternatively you will need to replace the line breaks with a more suitable character, such as whitespace.

Upvotes: 4

Related Questions