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

2 Answers 2

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")); ?>
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Odil Sattorov.. im learning yii now only.. here in my case, the 'register_script_name' is the javascript file ??
No this is a unique identifier of the script. You can put any name there.
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 Not working is big thing, Open the firebug and tell the error.
If you are using Firefox get firebug addon and open it and reload the page and you should see error, give us the error can help us to help you
|
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()')); ?>

2 Comments

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
still not working, am i need to include anything to make javascript to work. no reaction is taken

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.