0

I'm trying to use JavaScript in my page. My JavaScript code is:

function edit(){
  alert("test"); 
  return document.getElementById("confirm").value="";
}

Calling this, by onclick function

<?php echo CHtml::button("Edit",array('title'=>"Edit",'onclick'=>'js:edit();')); ?>

both where in same page and confirmation.php

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
sher
  • 21
  • 1
  • 2
  • 8

2 Answers2

4
Yii::app()->clientScript->registerScript('register_script_name', "
    $('#editButton').click(function(){
       alert('edit');
       return false;
    });
", CClientScript::POS_READY);

<?php echo CHtml::button("Edit",array('title'=>"Edit",'id'=>"editButton")); ?>
  • Thanks Odil Sattorov.. im learning yii now only.. here in my case, the 'register_script_name' is the javascript file ?? – sher Mar 01 '13 at 08:41
  • No this is a unique identifier of the script. You can put any name there. – Odiljon Sattarov Mar 01 '13 at 09:40
  • hi odil sattorov, still which is not working for me, i dont know where i did mistake.. what else i need to add.. this script is on the page, i didnt make it as a file. kindly help me – sher Mar 04 '13 at 03:20
  • @sher Not working is big thing, Open the firebug and tell the error. – Elbek Mar 04 '13 at 18:59
  • 1
    If you are using Firefox get [firebug addon](https://addons.mozilla.org/en-US/firefox/search/?q=firebug&appver=&platform=) and open it and reload the page and you should see error, give us the error can help us to help you – Odiljon Sattarov Mar 04 '13 at 19:08
  • i got the following warning, Use of globalStorage is deprecated. Please use localStorage instead. Use of attributes' nodeName attribute is deprecated. Use name instead. Use of attributes' nodeValue attribute is deprecated. Use value instead. – sher Mar 05 '13 at 02:12
  • Also i have the report no script on the page.. but i have the script in the page – sher Mar 05 '13 at 02:16
  • The warning that u gave has nothing to do with the script. The warning must belong to something else(some other script) – Odiljon Sattarov Mar 05 '13 at 06:17
1

Set an ID for your button:

<?php echo CHtml::button("Edit",array('title'=>"Edit",'id'=>'myButton')); ?>

JS:

window.onload = function() {
    document.getElementById("myButton").onclick = function() {
        alert("test");
        document.getElementById("confirm").value = "";
        return false;
    }
}

Or you can just remove the js: part from your code:

<?php echo CHtml::button("Edit",array('title'=>"Edit",'onclick'=>'edit()')); ?>
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • Thx Samuel liew, not working bro.. actually my code was working, later i renderPartial from another page, thn only javascript not calling, except this all working – sher Mar 01 '13 at 03:25
  • still not working, am i need to include anything to make javascript to work. no reaction is taken – sher Mar 01 '13 at 08:31