1

I am created a dashboard widgets using wp_add_dashboard_widgets() in admin area and created there a button ,i want when i click on button my script to be worked while that not working , all code of dashboard widgets putted in functions.php file and also written the script in functions.php file , what have you any solution where i can put the javascript by which my click events can work?please guide me.

this is my javascript code inside functions.php file.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

$('.dash_appoint_r').on('click', function(){
    alert('hello');
    }); 



</script>

and this is my php code where a button avilable as i told you.

<?php
    foreach($result as $all_records)

    {
                    // if($all_records->appointment_status==pending)
                    // {
                        // $pending=$all_records->appointment_status;
                    // }
                    // else
                    // {
                        // $pending='pending';
                    // }
        ?>

                  <tr>
                    <td><?php echo $all_records->id; ?></td>
                    <td><?php echo $all_records->patient_name; ?></td>
                    <td><?php echo $all_records->patient_email; ?></td>
                    <td><?php echo $all_records->patient_phone; ?></td>
                    <td><?php echo $all_records->appointment_date; ?></td>
                    <td><?php echo $all_records->appointment_doctors; ?></td>
                    <td><?php echo $all_records->appointment_status;?></td>
                    <td>
                    <select id="ack_status">
                    <option value="pending">pending</option>
                    <option value="success">success</option>
                    </select>
                    </td>
                    <td><button type="button" >submit</button></td>
                  </tr>
        <?php

    }
    ?>
                </tbody>
              </table>
              </div>
              </div>
        </div>
    <?php
}
?>

please,can you please tell me what i am doing wrong and where i need to put the javscript ?

1

You need to add your script into your functions.php

function my_theme_scripts() {
    wp_enqueue_script( 'my-great-script', get_site_url() . '/js/my-great-script.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'admin_enqueue_scripts', 'my_theme_scripts' );

Then, in your file (my-great-script.js), you will have to have something like this:

jQuery('.dash_appoint_r').on('click', function(){
    alert('hello');
}); 
4
  • loading is working ,but click event is not working why please guide me? Aug 15 '18 at 19:01
  • i checked after putting only alert('hello') in my-great-script.js then it working while this code is putting jQuery('.dash_appoint_r').on('click', function(){ alert('hello'); }); into the my-great-script.js then not working,what can be problem ? Aug 15 '18 at 19:05
  • to check the problem, use the full code, click the button and check the browser console to see what is happening. Aug 15 '18 at 19:16
  • getting this error "Uncaught TypeError: $ is not a function at my-great-script.js:2" but i don't know how to resolved it ?can you please help me? Aug 16 '18 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.