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

Delete the div element upon clicking the .close button

Is there a way to remove the div that contains the .close link when the .close link is clicked? I attempted using $(this).closest('div').remove() but it didn't work as expected... $(".close").hide(); $(".delete-link").bind("click", function ...

Conceal a button using an AJAX POST request

I'm encountering an issue with my Ajax post where I am trying to disable the button used to submit data. I've reviewed my code and it seems accurate, but the button is not getting disabled. I attempted using $("#refreshButton").attr("disabled", t ...

Struggling to figure out the correct method for aligning calendar using bootstrap-datetimepicker?

Struggling with aligning the calendar correctly under a textbox while using bootstrap-datetimepicker in a form? The positioning seems off, and the calendar icon isn't where it should be. It's not behaving like a simple datetimepicker as shown in ...

Capture each touchdown and convert it to currency format

As a novice in the field of JS, I have put in a lot of effort into learning from various sources such as websites, documentation, friends, and other questions on Stack Overflow. However, despite all my efforts, I seem to be stuck at this point. $(docume ...

Preventing Javascript array elements from being overwritten: Best practices

My challenge lies with the addToClients() function, which is designed to add a new value to the clients array whenever a button is pressed. The issue I am facing is that each time I press submit, the previous element in the array gets replaced by the new o ...

Is it possible to modify the appearance of the element that was just clicked on?

Hello everyone, I'm currently working on a form with different inputs, all with the same class: <input type="text" name="username" class="field" /> <input type="text" name="email" class="field" /> I'm trying to figure out how to ch ...

Displaying the Variable Product Options in WooCommerce

I have been working on creating a query to showcase products along with relevant information, but I am facing some challenges. Specifically, I am struggling with displaying the variable product options for all products and implementing an add to cart butt ...

Develop a real-time preview of the form inputs

While working on a multistep form, I am exploring the idea of creating a live preview that shows values entered into different input fields, text areas, and radio buttons. Currently, I have successfully built the form and made some progress using the foll ...

Unable to adjust the canvas size or scale using Fabric.js

Is there a way to display a large canvas as a smaller one for better user interaction without compromising the size of the uploaded canvas later on? I'm currently working with Fabric.js and exploring the Canvas CSS trick that involves setting the widt ...

What are some effective replacements for SoundManager2 worth considering?

While Soundmanager2 is a useful app, many find the code to be overly complex and difficult to navigate. It almost seems as if it was purposely written in a way to confuse rather than clarify. Are there any recommended alternatives to Soundmanager2 that are ...

Creating a Slider with a Multitude of Images

I am working on a project to develop a gallery that pulls images from a JSON source. I have successfully implemented infinite scrolling to display the images on the first page. However, I am now facing a challenge when it comes to showing the selected imag ...

Moving Angularjs table rows to another tableMoving table row from one Angular

I need a solution for transferring rows from one table to another. Here are the two tables involved: First Table: <table> <tr ng-repeat="row in items"> <td>{{row.PRODUCTDESCRIPTION}}</td> <td><inpu ...

Displaying outcomes in dialog box when button is pressed

I am working on a website where I want to enhance the user experience by displaying output in a dialogue box upon click. The current setup involves the user selecting a vendor and time duration, with the results appearing below the Submit button. However, ...

Encountering a problem with Selenium when dealing with tabular data displayed in a DIV format on a website, where each row is encapsulated within its own DIV element

While creating Selenium automation tests for a website with multiple rows contained within a main DIV element, each row represented by a separate DIV. For example, if there are 5 dynamically generated rows, the HTML code structure would look like this: < ...

Choose everything except for the list and its heading

I have a portion of HTML code on my hands. <div class="ProductDescriptionContainer"> <p>This handsome Clergy Shirt has a trim low neckband with two reinforced buttonholes, one at the front, one at the back, to align with Ziegler's Collare ...

Create a CSS style to set the background with a half height positioned behind the

I am looking to create a div with a background and overlay effect. Here is what I have done so far: I want it to look like this: body { background: blue; } .wrap { height: 300px; width: 600px; margin: 0 auto; background-color: #000000; } .b ...

What is the process for updating a placeholder text after the user makes a guess or enters

My latest project involves creating a fun guessing game where players have to identify the driver based on the teams they have driven for. The game displays the number of guesses allowed and keeps track of how many attempts the player has made so far. For ...

Hide in certain zones, contextual menu

I recently discovered how to create my own context menu with links based on the div clicked (check out here for more details). However, I am facing an issue now. I am struggling to prevent the context menu from appearing above the specific div boxes where ...

UI binder is having difficulty resolving CSS

After creating a search panel for my application using UI binder, I noticed that the desired behavior is not being achieved. Ui.xml <g:HTMLPanel> <c:SimpleContainer> <c:InfoContainerHeader text="{labels.searchFilter}" /> ...

a tag's functionality remains unchanged by its class association

In my Laravel project, I am attempting to create an active class for a link tag, but the class is not affecting the link. Below is the code snippet from my blade file: <li class="{{ (request()->is('categories*')) ? 'side-active&ap ...