1

I have tried to use like this

$(nTd).html("<div class='btn-group'> <button class='btn btn-info btn-sm' onclick='viewskpd('"+oData.id+"')' data-toggle='tooltip' title='Detail SKPD'><i class='fa fa-eye' style='color:white'></i></button>");

But it went like this:

<button class="btn btn-info btn-sm" onclick="viewskpd(" 00cc72988f8240428b25ac5327b2a3c6')'="" data-toggle="tooltip" title="" data-original-title="Detail SKPD"><i class="fa fa-eye" style="color:white"></i></button>

But I am not sure why the onclick function that trigger viewskpd() didnt work.

I have checked the console and it tell me like this.

Uncaught SyntaxError: Invalid or unexpected token

Why is this happen ?

2
  • I think your quotes around +oData.id+ just needs to be single quotes? so ('+oData.id+') Commented May 8, 2018 at 3:59
  • it make the javascript variable to be like html @AJDEV Commented May 8, 2018 at 4:02

5 Answers 5

3

The oData.id needs to be quoted so it's a string that is quoted with different quotes than enclosing the onclick attribute:

onclick='viewskpd(\""+oData.id+"\")'

In full:

$($0).html("<div class='btn-group'> <button class='btn btn-info btn-sm' onclick='viewskpd(\""+oData.id+"\")' data-toggle='tooltip' title='Detail SKPD'><i class='fa fa-eye' style='color:white'></i></button>");
Sign up to request clarification or add additional context in comments.

4 Comments

okay ihave edited my question regarding your answer, and it stll didnt work
coud you explain to me with \"" and "\" thing ? first time i see this kind of quoted
@Gagantous, Actually you need to know more about RegExp, find a good source and read about it, in your case you can use toString function to avoid complexity.
That's not a regular expression @AmerllicA. It's just escaping the quote character that's used to start and end the string: ('_\'_' to get _'_ or "_\"_" to get _"_). There are a few other special characters that you'll need to escape to use like a newline, \n. Have a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2

You're entering some pretty wild territory... script injected into markup attributes by scripts which were probably originally inside of markup language itself. Don't do this, it's a nightmare.

Try something like this instead:

<button data-action="viewskpd" data-skpd-id="00cc72988f8240428b25ac5327b2a3c6" ...

In your script...

$(body).on('click', 'button[data-action="viewskpd"]', (e) => {
  console.log(e.target);  // 
});

Untested, but that should get you started. Put your data in the markup, but your script in your script. Keep them separate.

2 Comments

ohhh, i never thought about that..., why did you used (e) => { i thought it's function(e){
@Gagantous I'm in the habit of using the ES6 syntax. function(e) { } works just fine here as well.
1

Why don't you assign an ID to <button class='btn btn-info btn-sm' ... and program its click event in JS?

At whereever the location that you are having oData.id, you could do something like follows.

<button id='id1' class='btn btn-info btn-sm' ...

Then use this id1 as the selector and program that element's click event, which would be like,

$(document).on("click", "#id1", function(){
    viewskpd(oData.id); // Call the required function
});

Comments

1

Actually you need a string variable inside viewskpd function. Use toString() function for it, you must write like below:

$(nTd).html("<div class='btn-group'> <button class='btn btn-info btn-sm' onclick='viewskpd("+oData.id.toStirng()+")' data-toggle='tooltip' title='Detail SKPD'><i class='fa fa-eye' style='color:white'></i></button>");

The 00cc72988f8240428b25ac5327b2a3c6 has no meaning for JavaScript until you turn it to string .

Comments

1

Alternatively you may use `. Please note that this is a specific character different from " and '. Then enclose the variable into ${}, it allows better readability:

let ntd = document.getElementById("ntd");
ntd.outerHTML = `<div class='btn-group'> 
                 <button class='btn btn-info btn-sm' 
                 onclick='viewskpd("${oData.id}")'
                 data-toggle='tooltip' title='Detail SKPD'>
                 <i class='fa fa-eye' style='color:white'>
                 </i></button></div>`;

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.