Paolo Rossi
Paolo Rossi

Reputation: 2510

Call jquery function from php page

I created a jquery function in a js file that is included in a php page.

Js file

$( function() {

    function disab(){
        $('input#idname').attr('disabled', 'disabled');
    }

});

In my php file I tried to call the disab function in this way but with no success.

echo "<script>";
echo "$(function(){";
echo "disab();";
echo "});";
echo "</script>";

How can I do this? thanks

Upvotes: 1

Views: 16008

Answers (1)

bipen
bipen

Reputation: 36551

first of all put your funciton outside the document.ready function in your js file...so that it is global and can be accessed from anywhere.

 function disab(){
    $('input#idname').attr('disabled', 'disabled');
 }

 $( function() { .. }); //use if needed..

and call the function inside <script> tag in php file

 <?php
 //all you php related codes
 ...
 ?>
 <script>
    $(function(){
        disab();
    });
 </script>

and most important, use prop() instead of attr() ... if you are using latest version of jquery (jquery 1.6+ )

  $('input#idname').prop('disabled', true);

Upvotes: 3

Related Questions