-1

I am trying to add an <a> </a> inside of my datatable where I already had o use the ""´s right after return, but how do I pass my sub into my alert call?

function (data, type, full, meta) {
                    var sub = full[7].substring(0,21)+"...";
                   return "<a class='astyle'  onclick='new function() { alert('"+sub+"') }'>"+sub+"</a>";

                                }

My example is not working.

1 Answer 1

2

You have to escape the ' here:

onclick='new function() { alert(\'"+sub+"\') }'>

Full Code

function (data, type, full, meta) {
  var sub = full[7].substring(0, 21) + "...";
  return "<a class='astyle' onclick='new function() { alert(\'"+sub+"\') }'>"+sub+"</a>";
}

Best way to handle these would be split all the variables and get it done. Don't have one single variable with all the confusing quotes and codes.

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

4 Comments

Yeah I tried this variant too and I copied your code too but weird enough it still doesnt work, second ' in alert doesnt get highlighted at all even in my PHP Storm
@ChrisFodor Don't just go with your editor boss. Editor is not a PHP interpreter and will not be powerful enough to suggest the runtime issues. It won't get syntax highlighted. But if you don't do this, it will throw an error in the front end. If you say, this doesn't work - for the editor highlight, please don't! It never works.
I understand that, but the alert doesnt show up either way. Edit: I got it to work like this: return "<a class='astyle' onclick='new function() { alert(\""+sub+"\") }'>"+sub+"</a>";
@ChrisFodor Super... Thanks for letting me know... :D Have a nice day.

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.