Jquery on-click method parameters comma not supported

Viewed 96

I got dynamic onclick with 2 parameters (id and name).

Sample data

id   name
1    foo bar 
2    hi foo's, where is bar
3    hello foo

js code

// django for loop
{% for original in data %}
    <button onclick="click_new('{{original.id}}', '{{original.level_name}}')">Click New</button>
{% endfor %}


function click_new(id, name){
   alert(name)
}

Uncaught SyntaxError: Unexpected end of input

for hi foo's, where is bar

enter image description here

https://jsfiddle.net/32wzcr4k/

2 Answers

Alternate solution would be setting second value as data-attribute and inside function pass this to identify button which is clicked .

Demo Code :

function click_new(id, el) {
  alert($(el).data("name") +" -- "+id)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--<button data-name="{{original.level_name}}" onclick="click_new('{{original.id}}',this)">Click New</button>-->
<button data-name="foo's, where is bar" onclick="click_new('1',this )">Click New</button>
<button data-name="foo" onclick="click_new('2',this )">Click New</button>

Hi looks like your function is messing with a single quote and double quote.

your HTML should be something like this.

<button type='button' onclick="click_me(1,'hi foo\'s where is bar')">
11
</button>

here is working fiddle

Related