- Color a
span/divelement content when a user moves the mouse over the element.
- Use prompt to read a value from user and display it in the span element.
Click to enter text!
<script> function displayUserInput() { var userInput = prompt('Enter some text:'); if (userInput !== null) { // Check if user clicked OK or Cancel document.getElementById('userInputDisplay').textContent = userInput; } } </script>- Display the mouse X and Y coordinates in a
<span>tag when you click on a<h1>tag which contains a paragraph.
<script> // Get the heading element var heading = document.getElementById('headingWithParagraph'); // Add click event listener to the heading heading.addEventListener('click', function(event) { // Get mouse X and Y coordinates relative to the document var mouseX = event.pageX; var mouseY = event.pageY; // Get the span element to display coordinates var coordinatesSpan = document.getElementById('mouseCoordinates'); // Update the span content with coordinates coordinatesSpan.textContent = 'Mouse X: ' + mouseX + ', Mouse Y: ' + mouseY; }); </script>
-
Write a Javascript code for character counts in the
textarea.
<textarea id="inputText" rows="4" cols="50" placeholder="Type something..."></textarea>
Characters: 0
<script>
// Get the textarea element
var textarea = document.getElementById('inputText');
// Get the div element where character count will be displayed
var charCount = document.getElementById('charCount');
// Add input event listener to the textarea
textarea.addEventListener('input', function() {
// Get the current value of the textarea
var text = textarea.value;
// Count the number of characters
var count = text.length;
// Update the character count display
charCount.textContent = 'Characters: ' + count;
});
</script>
- Convert a given number from decimal to binary or hexadecimal function decimalToBinary(decimalNumber) { return (decimalNumber >>> 0).toString(2); }
// Example usage:
var decimalNumber = 123; // Replace with your decimal number
var binaryNumber = decimalToBinary(decimalNumber);
console.log(Binary representation of ${decimalNumber}: ${binaryNumber});
function decimalToHexadecimal(decimalNumber) { return decimalNumber.toString(16).toUpperCase(); }
// Example usage:
var decimalNumber = 123; // Replace with your decimal number
var hexadecimalNumber = decimalToHexadecimal(decimalNumber);
console.log(Hexadecimal representation of ${decimalNumber}: ${hexadecimalNumber});
- With Javascript write a simple from validation
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<span id="emailError" class="error-message"></span><br><br>
<input type="submit" value="Submit">
- In your HTML Add two buttons, where first button action for full screen mode and the second button for exit from full screen mode.
This is some example content.
<button onclick="toggleFullScreen()">Enter Fullscreen</button>
<button onclick="exitFullScreen()">Exit Fullscreen</button>
- When user press any key in your html page show a alert that
Invalid Key Pressed, but when user press spacebar show an alertThank You..!!and close the current window.
- When a cursor is moved over an content, allow the user to edit the content in HTML page.
NOTE: Input element should not be used.
Hover over this content to edit it.
<script>
function makeEditable(element) {
element.setAttribute('contenteditable', true);
element.focus(); // Automatically focus on the element to start editing
}
function makeNonEditable(element) {
element.removeAttribute('contenteditable');
}
</script>
