1

I faced a problem with laravel blade template with javascript.

I have textarea and cancel button that return the textarea value to it's original value. But when the original value have multi-line, there error in script, because the blade print the value into the new line not \n in value.

Blade syntax:

$('#my-textarea').val('{{ $oldValue }}');

In html page source:

$('#my-textarea').val('old value with multiline');

That one will cause error syntax in javascript Uncaught SyntaxError: Invalid or unexpected token

Expected result:

$('#my-textarea').val('old\nvalue\nwith\nmultiline');

The question is, how can i print the value without being converted to actual new line but char \n?

codecarver
  • 249
  • 2
  • 10

1 Answers1

0

Add following code in your blade if you have installed "laravelcollective/html" package:

{{ 
    Form::hidden(
        'originalValue',
        $oldValue,
        array(
            'id'=>'originalValue'
        )
    ) 
}}

Or add following code:

<input type="hidden" value="{{$oldValue}}" id="originalValue">

And your jQuery will be :

$('#my-textarea').val($('#originalValue').val());
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • I use your method at first, but i found another trick on the way. I am not sure it's safe to use this, but it's working for me: ```$('#my-textarea').val({!! json_encode($oldValue) !!});``` without quote inside ```val``` function – codecarver Oct 23 '17 at 08:27