Troubleshooting the issue: Unable to shift a div to the left in a Rails app using jQuery and CSS when a mouseover event occurs

Hey there, I'm working on a div that contains a map. My goal is to have the width of the div change from 80% to 50% when a user hovers over it. This will create space on the right side for an image to appear.

I've been looking up examples of jquery event handling online and tried implementing it in my app. However, it seems like I may have missed something because there's no response to the mouseover action. If anyone could help me troubleshoot this issue, I would greatly appreciate it. I'm new to web development, rails, and jquery.

Here are the relevant files from my rails project:

list.html.erb - contains all the HTML

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=Q&sensor=false"></script>  
<%= stylesheet_link_tag 'application' %>  
<%= javascript_include_tag 'application'%>  
<%= stylesheet_link_tag 'listings' %>  
<body onload="initialize()">  
    <div id="control_panel">  
    <input type="button" onclick="getlistings();" value="Add Markers">  
    <input type="button" onclick="clearMarkers();" value="Remove Markers">  
    </div>  
    <div id="info"></div>  
    <div id="map_canvas" onmouseover="moveLeft();" ></div>  
</body>  

map.js.erb - contains the javascript and jquery functions

function moveLeft()  
{  
    $(function(){ $("#map_canvas").bind("mouseover", shiftLeft) });  
    $(function(){ $("#map_canvas").bind("mouseleave", shiftLeft) });  
}  

function shiftLeft(evt)  
{  
    $("#map_canvas").toggleClass("shifted_mapCanvas");  

}  

css file:

#map_canvas  
{  
  width: 90%;  
  height: 90%;  
  margin-left: auto;  
  margin-right: auto;  
  html   
  {   
    height: 100%   
   }  
  body   
  {   
    height: 100%;   
    margin: 10;   
    padding: 10   
   }  

}

.shifted_mapCanvas  
{  
  width: 50%;  
  height: 90%;  
  margin-left: auto;  
  margin-right: auto;  
  html    
  {   
    height: 100%   
   }  
  body   
  {   
    height: 100%;   
    margin: 10;   
    padding: 10   
   }  
}  

Answer №1

Addressing the specific question you've raised, it seems like you are attempting to utilize jQuery functions without loading jQuery itself. This could be causing the issues you're facing.

Instead of inline JavaScript calls, a better approach would be to use jQuery in a separate script block as shown below:

<script>
$('div#map_canvas').hover({
  function() { $(this).toggleClass('shifted_mapCanvas'); },
});
</script>

This method should work properly if jQuery is loaded correctly. Refer to the documentation for hover() and toggleClass().


Additionally, I'd like to point out some other areas that may need attention:

banditKing, respectfully, it appears there are fundamental concepts in HTML, CSS, and Rails that require clarification based on your recent questions.

Here's why:

1) Your Rails view file should follow the correct naming convention, such as show.html.erb or index.html.erb. Review the Rails Guide on Creating A Resource.

2) Avoid including body tags within individual views in Rails, as they belong in the layout file to maintain valid markup structure.

3) When working with SCSS, ensure your styling rules comply with valid CSS syntax. Nesting styles for HTML and BODY tags can lead to invalid CSS definitions.

Refer to How to Make a Simple Valid HTML Document for more insights on these topics.

It might also be beneficial to explore resources on unobtrusive JavaScript and deepen your understanding of HTML, CSS, and JavaScript from reputable sources like W3C Schools.

I recommend starting with Michael Hartl's Rails 3 Tutorial to strengthen your foundation in web development.

By investing time in mastering these basics first, you'll significantly reduce future challenges and enhance your proficiency in tackling projects effectively.

I urge you to dedicate effort to learning these fundamentals before progressing further with your current project.

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

Cookies are not sent by Ajax when used on IE10

After updating to IE10 on my Windows 7 machine, I have been facing an issue with logging into my application. Strangely, this problem did not occur with IE9 and it still works fine with Firefox and Chrome. The main issue seems to be that session cookies ( ...

Distinguishing between client side rendering and server side rendering is the backbone of web development

My goal is to give users the option to request webpages from my website either directly from the server or by clicking on links, which will be managed by Backbone's router. When a user requests a webpage directly from the server, they will receive a ...

Prevent a sliding panel from responding if there is no input text by incorporating jQuery

I have a straightforward example of user input and a button that reveals "Hello World" when clicked: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1 ...

The only functioning href link is the last one

Currently, I am in the process of constructing a website using HTML and CSS. My goal is to create a white rectangle that contains 4 images, each serving as a clickable link redirecting users to different sections on the page. The issue I am facing is that ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

Reverse the orientation of the SVG image, for instance, the graph

I've been experimenting with creating a bar graph using SVG, but I'm struggling to understand how it works. I tried rotating an existing graph upside down, but there seems to be a small offset. You can view the corresponding jsfiddle here - http: ...

Ways to show a corresponding number beneath every image that is generated dynamically

I have a requirement to show a specific image multiple times based on user input. I have achieved this functionality successfully. However, I now need to display a number below each image. For example, if the user enters '4', there should be 4 im ...

Picture transports during change

Is there a way to increase the size of images in a List without causing other images to move when hovered? Currently, when an image is hovered, it increases in size but causes the surrounding images to shift to a new line and I would like to avoid this beh ...

LabeFor does not automatically create the display-lable class attribute

When setting up a new MVC project, the default Site.css file typically includes the following styles: /* Styles for editor and display helpers ----------------------------------------------------------*/ .display-label, .editor-label { font-weight: ...

Personalized element IDs and unique CSS styles

After editing the code snippet below... <div id="rt-utility"><div class="rt-block fp-roksprocket-grids-utility title5 jmoddiv"> I applied the changes in my custom.css file like this: #rt-utility .rt-block {CODE HERE} However, when attemptin ...

modify the color of a box within a grid upon clicking it

I am curious if it is possible to change the color of a box when it is clicked. I am working on coding a "calculator layout" and would like to start with this part 1 2 3 4 5 6 7 8 9 0 This is what I have been working on and struggling with. $(docume ...

Is the POST AJAX request being rejected due to CORS restrictions?

I have configured CORS on my node.js server running on port 5000 in the following way: var app = express(); app.use(cors()); //normal CORS app.options('*', cors()); //preflight app.post('/connect', function(req, res) { console ...

Be patient for the execution of the addClass function

I'm currently facing an issue with jQuery. My goal is to make a single button perform two different actions based on the class. Despite its apparent simplicity, I've hit a roadblock in achieving this functionality. Here's what I have so far: ...

Unable to retrieve DOM value due to Vue.js template being inaccessible in Chromium, including from both DevTools and extensions

Currently, I’m developing a Chrome extension that needs to retrieve specific values from a webpage such as the item title. However, instead of fetching the actual title, it is reading a Vue.js template variable. Even when I use DevTools to inspect the p ...

What's the best way to navigate across by simply pressing a button located nearby?

Hey there! I'm new to JavaScript and I'm working on a shopping list page for practice. In my code, I can add new items to the list, but what I really want is to be able to cross out an item when I click the "Done" button next to it, and then uncr ...

When using Jsoup, make sure to nest the <div> tag within an <a>

As per the findings in this response: Referring to HTML 4.01 guidelines, <a> elements are limited to inline elements only. Since a <div> is a block element, it should not be placed within an <a>. However... In HTML5, <a> elements are all ...

Tips for correctly identifying and sending the form name and submit button name when using AJAX for form submission

I need a concise solution to include the form name and submit button name along with the form input fields when sending data. I have attempted to use the form.serializeArray() method, but it only retrieves the input field names in the post data. Is there a ...

[quicksearch] Finding the amount of rows being displayed - simple tricks to know the count

I've successfully implemented TableSorter and QuickSearch plugins with jQuery. Now I'm looking to enhance my table by: Show row numbers dynamically for each displayed row Show the total number of displayed rows somewhere on the page ...

html form shifting positions based on screen resolution

I have been experimenting with a login screen design for my website recently. I created a form with a fixed position, but as the screen resolution changes, not only does the form's position shift, but also the image moves, causing an unwanted interse ...

What is the best way to implement this header style with code?

I came across this design from a potential client recently, even though we didn't end up working together. I thought it would be a good exercise to try coding the design for practice purposes. Just to clarify, this is not my original design and I have ...