0

I got weird on string concat in jQuery as follow :

      var name = button.data('name')
      var emailName = button.data('email')
      var userName = button.data('username')
      var userStatus = button.data('active')
      var userId = button.data('id')
      alert(userId)
      var actionUpdate = "{{action('UserController@update', "+userId+")}}" 
      alert(actionUpdate)

and result/alert for userId is 28723050-71e9-11e7-a0a9-e9f620359699 (uuid as user id) but when I concated/joined, I got actionUpdate variable is {{action('UserController@update', "userId")}} instead of {{action('UserController@update', "28723050-71e9-11e7-a0a9-e9f620359699")}} as what I expected.

I tried to join those string and variable with concat or +=, but didn't get a result as expected.

Any alternatives and solution is much appreciated.

2
  • 3
    Double check that that is the exact concatenation that you are doing. I'm not seeing a syntax error. Actually it can't be exactly what you are doing, as you said the result has " in it, where as the example you gave would not include double quotes in the result Commented Jul 27, 2017 at 20:47
  • Yes, you are right, I tried in JSfiddle and work well also, my purpose is make an update user in bootstrap modal with laravel, so I need this userId as variable instead of "userId" as string.. Commented Jul 28, 2017 at 7:20

2 Answers 2

1

If, for some odd reason, what you really want is

{{action('UserController@update', "28723050-...9699")}}

with single-quotes around the UserController@update and double-quotes around the userId, I think you need

  var userId = button.data('id')
  alert(userId)
  var actionUpdate = "{{action('UserController@update', \""+userId+"\")}}" 
  alert(actionUpdate)

(i.e., you need to add an escaped double-quote character on either side of the concatenation part.)

Sign up to request clarification or add additional context in comments.

1 Comment

it works well as expected on jsfiddle.net/nprasetyo/2gnusjgt, but still problem on my application, tried userId.toString(), but still no luck lol
0

Thank you John, for your idea about "escaped char", this work as I expected now, I guess "{,{,},}" has special meaning in JS or Jquery, so I wrote this line as :

var actionUpdate = "{{action('UserController@update', '" + userId + "')}}"

hope it can be useful for who want to change form submit action to laravel controller.

1 Comment

I mean, prefixed with \ (backslash before {,}) but didn't show up above.

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.