How can I alter the border of a div when hovering over it?

Greetings everyone, I am looking for a way to change the border of a div on mouse hover, with the added challenge that the id is dynamically changed. Any help or guidance would be greatly appreciated. Thank you!

Answer №1

Is this the solution you're looking for?

section {}
section:focus {border:1px solid #000000;}

Answer №2

To ensure cross-browser compatibility, it is recommended to use JavaScript instead of the :hover pseudo class. Older versions of IE may not support :hover on elements that are not anchor tags.

An alternative approach is to assign a class to the div and then use jQuery for easy selection. This method is straightforward and effective.

For example:

$(function(){

  $('.yourclass').hover(
    function(){
       $(this).addClass('hovered');
    },
    function(){
       $(this).removeClass('hovered');
    }
  );

});

The class 'hovered' can contain the CSS styles for the hovered state.

If you wish to change the ID upon hover, you can include $(this).attr('id','newID'); in the code above.

Answer №3

I hope this information proves useful to you.

<div onmouseover="somefunction(<?php echo $divid ?>)"> </div>


<script>
function somefunction(id)
{
document.getElementById(id).style.border="10px #FF0000 solid";
}
</script>

Answer №4

CSS offers the :hover selector, which can be applied to a div element.

For instance:

div:hover { border: 1px solid #454545; }

To dynamically change the id, I would suggest using jQuery

jQuery(this).attr("id",newId);

If you provide specific code, I can assist with the syntax for your particular situation...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vuex action method completes without returning a value

I've come across a situation in my Vuex action where the console.log() is displaying an undefined output. Here's the method in my Vuex action: const actions = { async fetchByQuery({ commit, title }) { console.log(title); //other code ...

Determining the specific selector that triggered an event in jQuery when using multiple selectors

I have a question about handling events with multiple selectors. For example: $('.item a, .another-item a').click(function(e) { }); Is there a way to identify which parent selector triggered the event? In this case, was it .item or .another-it ...

Transforming the text to a new line

I have been attempting to format lengthy texts from a JSON file, but I haven't been successful. The text keeps displaying as multiple sections within a single response, which can be seen in the image below. I've tested solutions like word-break a ...

"Troubleshooting: Issue with React's useState and useEffect functions in conjunction with localStorage

Encountering a challenge with a React component using useState and useEffect hooks to handle state and maintain it in localStorage. The component aims to create a collapsible section with a heading and content, where the expanded state is stored in localSt ...

Is it possible to execute a function when the AJAX request is successful and returns a status code of

I am looking to implement the success function to only run a certain function if the status code is 200. I have come across this example: $.ajax ({ success: function(data,textStatus,jqXHR){ external(); } )}; However, I have not found a clear ...

Execute an asynchronous request using Javascript to communicate with a Spring Controller

I've created a JSP page that includes some JavaScript code: function sendData(tableID) { var table = document.getElementById(tableID); var dataArray= new Array(); for (var i = 1;i<table.rows.length; i++){ var row = table. ...

The Angular package functions correctly when tested locally, but encounters issues after deployment

In my Angular application, I have incorporated the ng2-jalali-date-picker and jalali-moment. While the date picker functions properly on my local machine, after building and deploying the application to a server using the command: ng build --prod, I encou ...

I am struggling to figure out how to manage input in my project

I'm currently working on a project involving to-do lists, and I've encountered an issue with adding comments to my list items. Here is the current setup: App flow Once a list item is added, you should be able to click on the item and a new wind ...

Creating a blur effect in three JS using a WebGL shader

Feeling a bit lost here, but eager to learn. I recently switched from P5.js to Three.JS for a project and ran into some challenges with applying shaders. In P5.js, it was simple to create a canvas and add a shader, but in Three.JS, it's not as straigh ...

Solving the enigma of CSS positioning: mastering floats, margin-left/right, and auto

Hey there, I'm having trouble posting an image but here is the link: I believe this explanation will be clearer, D2, D3 & D4 need to be aligned next to each other and centered on the screen at all times. (960px)      & ...

Inquiries about ngshow and the scope concept

I have a question about using AngularJS. I have multiple sections and only want to display one at a time using <section ng-show="section6_us"> </section> and <section ng-show="section7_us"> </section>. My scope has many variables. ...

Utilizing Angular 6's Mat-Table in a dynamic way

I am currently working on an Angular application that involves displaying data fetched from an API. The challenge I'm facing is that I do not have prior knowledge of the data I will be retrieving. Additionally, I need to create a model that can be use ...

How to Preserve Previous Value in a jQuery Change Event for an <input> Element?

Today I spent some time searching for a simple solution, but unfortunately, I have not found one yet. Essentially, what I am trying to do is capture a change in an input element while also being able to retrieve the previous value. Below is a basic exampl ...

Retrieve a document from an HTTP NodeJS server

Currently, I am working on a nodejs application that involves a http server responding to incoming POST commands. One task I need to accomplish is returning an entire binary file. The traditional method would be using: response.end(file, 'binary); // ...

Point light in Three.js is not functional when used with oversized meshes

Experiencing an issue with using Three.js point light in my project. Here's what I have: var color = 0xffffff; var intensity = 0.5; var distance = 200; position_x = 0; position_y = 0; position_z = 0; light = new THREE.PointLight(color, int ...

Using JavaScript to parse Java objects into JSON data

I'm working on sending an array of POJOs as a response to an AJAX call. Within my POJO, the following toString() method is defined: @Override public String toString() { return "Expense [period=" + period + ", description=" + description + ...

Insert an HTML page into a div element

I am having an issue with my angular template. I have a div where I am attempting to load an HTML view file (.html) based on a $watch event, but for some reason, the HTML view is not being loaded into the div. Below is a snippet of my controller code that ...

Guide on retrieving POST data in sveltekit

Hello, I'm new to sveltekit and I am trying to fetch post data in sveltekit using a POST request. Currently, I am utilizing axios to send the post data. const request = await axios .post("/api/user", { username, email, ...

I am having trouble getting Nodemon to load my index.js file

I've been facing some issues with getting my code to run smoothly. Every time I try to use nodemon to launch it, it attempts to connect but I just see a loading icon in the localhost tab. When using live-server, the homepage loads fine on its own but ...

Bugs in resizing and calculating height

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ut ...