0

I am using Laravel Collective form. I want to use onclick analytic.js but the browser shows me an error. here is the code

<div class="field-name">
{{--<%= f.text_field :name, placeholder: 'your name', :onclick => "ga('send','event', 'form-lp-input', 'step3-name', '', 0);" %>--}}
{!!Form::text('name','yes',null,['placeholder'=>'yourname','onclick'=>'ga('send', 'event', 'form-lp-input', 'step2-zip',
 '', 0);'])!!}
</div>

here is the output

sharna
  • 31
  • 7

2 Answers2

0

There are two problems in your code:

  • Form::text() only uses 3 parameters, you are passing 4:
public static function text($name, $value = null, $options = array())
  • You have to escape your single-quoted javscript string or use double-quotes:

try this:

{!!Form::text('name', 'yes', ['placeholder' => 'yourname', 'onclick' => 'ga("send", "event", "form-lp-input", "step2-zip","", 0);'])!!}
Remul
  • 7,874
  • 1
  • 13
  • 30
  • this solved my problem but can you give me some more explanation about the \ you used? – sharna Nov 29 '18 at 16:57
  • I updated my answer, you can escape the single-quoted javascript or use double-quotes. The problem is you are using a single-quoted string so you cant use another single-quote inside without breaking it. [Read more here](https://stackoverflow.com/a/3446286/9193055). – Remul Nov 29 '18 at 17:07
0

I see I'm answering a bit late :) but still, maybe this solution will help someone...

To escape quotes you can use \Illuminate\Support\HtmlString class as following:

{!! Form::text('name', 'yes', ['placeholder' => 'yourname', 'onclick' => (new \Illuminate\Support\HtmlString("ga('send', 'event', 'form-lp-input', 'step2-zip', '', 0)"))]) !!}
lubart
  • 1,746
  • 3
  • 27
  • 35