strange behavior chtml :: link

hello all,

I have a problem with this button chtml :: link, confirm by entering as a parameter.

The button is created dynamically in a div after a form, and the button calls a javascript function.

If I leave the parameter confirm:

    echo CHtml::link('testBtn', "", array(

    'style' => 'cursor: pointer;',

    "confirm" => "are you sure?",

    "onclick" => "{test();}"));

It does not work

I take it out if it works.

    echo CHtml::link('testBtn', "", array(

    'style' => 'cursor: pointer;',

    //"confirm" => "are you sure?",

    "onclick" => "{test();}"));

previously created divs on page load function normally with the confirm, only the div + button does not work with dynamically created confirm or not the function is called JavaScript

the test javascript function have a simple alert

Thanks a lot


echo CHtml::link('testBtn', "javascript:;", array(

    'style' => 'cursor: pointer;',

    "onclick" => "doSomething(); return false;"



function doSomething(){

   if(!confirm("Are you sure ?")){

      return false;


   alert("Seems like you are sure!!!");



hello twisted1919 ,

your suggestion works. But because my work does not confirm? I confirm with the different buttons, and everything is working properly.

Thanks again, you’re a sniper. :D

i don’t really know why isn’t working as you do it… however, you should know that it is way better to have your scripts in separate JS files than in your view code. And also, i usually don’t use the on* family for events, because i have jQuery and i can separate the events in various js files, this has the huge advantage that you have all your js code in one place + you can later compress everything + it will be easier to maintain the code(and for separating the javascript code logic, i am learning backbone.js now)

you know, it’s all separated, I said what I call a function with alert, but in reality is a function that deletes from the database, and is written in a separate js file, and the same function is called from multiple views, in fact, confirm each has its own message. but I have to post something simple skeleton

ah, you’re on the right track then :)

;) thanks for the answers