2

I am trying to use a variable in a javascript function written as onClick attribute in button element but it's not working out

The javascript function I am trying to trigger when user click on the button element

updateForm(variableHere);

Variable is $dealer->id

This is how I am trying to do

@if ($dealer->user->activated==true)
    {{ sprintf(Form::button('%s', ['class' => 'edit', 'style' => 'border: none; background: none; padding: 0px;', 'onClick' => 'updateForm(%s)']),'<i class="fa fa-eye-slash"></i>', '{{{ $dealer->id }}}')  }}
@endif
{{ Form::open(['route' => ['dealers.status', $dealer->id], 'id' => 'update-status-'.$dealer->id ]) }}
{{ Form::close() }}

but getting this when I check the element's source code

<button fa-eye-slash"="" fa="" onclick="updateForm(&lt;i class=" style="border: none; background: none; padding: 0px;" class="edit">)" type="button"&gt;<!--?php echo e($dealer--->id); ?&gt;</button>

What am I doing wrong here?

2 Answers 2

2

First, the data passed to sprintf is backwards. Since the data used to fill in the attributes comes before the data to place into the button element, you would need to switch the second and third parameters.

Second, attempting to pass in the dealer variable like that will actually print out the PHP code, instead of executing it, so you would need to get rid of the blade syntax surrounding it.

Third, I think a better method to do what you're looking to do is to use the HTML::decode() method, instead of sprintf:

@if ($dealer->user->activated==true)
    {{ HTML::decode(Form::button('<i class="fa fa-eye-slash"></i>', ['class' => 'edit', 'style' => 'border: none; background: none; padding: 0px;', 'onClick' => 'updateForm('.$dealer->id.')'])) }}
@endif
Sign up to request clarification or add additional context in comments.

Comments

0

I just found a solution and feels like stupid

I can simple use it like this updateForm('.$dealer->id.') instead of calling sprintf function on updateForm()

so I just need to change it to this

@if ($dealer->user->activated==true)
    {{ sprintf(Form::button('%s', ['class' => 'edit', 'style' => 'border: none; background: none; padding: 0px;', 'onClick' => 'updateForm('.$dealer->id.')']),'<i class="fa fa-eye-slash"></i>')  }}
@endif
    {{ Form::open(['route' => ['dealers.status', $dealer->id], 'id' => 'update-status-'.$dealer->id ]) }}
    {{ Form::close() }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.