Write me a jquery script to log to console
- User
Write me a jquery script to log to console
Assistant
Here's a simple jQuery script that logs messages to the console with different scenarios:
<!DOCTYPE html> <html> <head> <title>jQuery Console Logger</title> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <style> body { font-family: Arial, sans-serif; padding: 20px; } .btn { padding: 10px 20px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; } .btn:hover { background: #0056b3; } </style> </head> <body> <h2>jQuery Console Logging Demo</h2> <button class="btn" id="logButton">Click to Log</button> <script> $(document).ready(function() { // Basic console log console.log("Document ready - Initial load log"); // Log on button click $("#logButton").click(function() { console.log("Button clicked at " + new Date().toLocaleTimeString()); // Log multiple arguments console.log("Multiple", "arguments", "example"); // Log object console.log({ event: "click", timestamp: Date.now(), element: $(this).text() }); }); // Log periodically setInterval(function() { console.log("Interval tick - " + new Date().toLocaleTimeString()); }, 5000); }); </script> </body> </html>This script demonstrates three types of console logging:
- Initial load logging when document is ready
- Click-triggered logging with timestamp
- Periodic logging using setInterval
Key features:
- Uses jQuery's document ready handler
- Includes modern CSS styling for the button
- Shows different console.log variations:
- Simple string message
- Multiple arguments
- Object logging
- Includes a setInterval example for periodic logging
- Responsive button with hover effect
To use:
- Save as .html file
- Open in browser
- Open DevTools (F12) and check Console tab
- Click the button to see click logs
- Watch the interval logs every 5 seconds
The console will show:
- Initial load message
- Click messages with timestamps
- Interval messages every 5 seconds
- Object inspection capabilities in the console
Note: Requires internet connection for jQuery CDN. For production, consider using a local jQuery copy.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.