Using a combination of Ajax and jQuery in Rails4 to showcase a date in the %m/%d/%yyyy format ultimately results in a split

Recently, I encountered an issue with an ajax call that retrieves a date from the database. The date is stored as a double in the database and then converted to a string. I used Date.parse to change it into a date object and used strftime to format it. However, a problem arose when updating the view using jQuery - it somehow interpreted '/' as division, resulting in a decimal value that was a result of month divided by day divided by year.

Below is the code snippet from my js.erb file:

$('#debug').html(<%= @sdate.strftime("%m/%d/%Y")%>)

Removing the '/' and keeping just "%m%d%Y" displayed the date correctly. I also attempted moving the formatting logic to the controller and outputting the result to the console, which showed the correct date. However, the issue persisted in the view due to how jQuery was interpreting the code. Can anyone provide guidance on how to resolve this issue so that the date displays correctly in the view? Thank you

Answer №1

Remember to include quotes around strings :)

$('#output').html('<%= @date.strftime("%mm/dd/yyyy") %>');

You can also use the .to_json method:

$('#output').html(<%= @date.strftime("%mm/dd/yyyy").to_json %>);

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

URL not passing on variable

Here is the code I have for a basic 'change email' script. I'm currently struggling to get it working and can't figure out what's wrong. <?php if (isset($_GET['u'])) { $u = $_GET['u']; } if (isset($_POS ...

tips for remaining in modal window post form submission

I have a form within a modal window. I am using ajax to load content in the same modal window, but the issue is that it redirects to the main page after submitting the form. How can I ensure that the modal window stays open even after the form submission? ...

Having trouble understanding why adding raw HTML is yielding different results compared to generating HTML using jQuery

I need assistance with two jsFiddles: http://jsfiddle.net/kRyhw/3/ http://jsfiddle.net/kBMSa/1/ In the first jsFiddle, there is code that adds HTML to my ul element, including an 'X' icon in SVG format. Attempting to recreate this functionali ...

Validate the date selected in a dropdown menu using JavaScript

I'm still relatively new to Javascript, just working my way through some tutorials. I have three select boxes in my HTML form as shown below. HTML Form: <table> <form id="enrolment" name="enrolment" onsubmit="return datevalidate();" action ...

Tips for Repurposing a React Table

I am in the process of developing my own react component library to be used across my entire application. At the moment, I have started with a table component which is currently undergoing testing. However, I am facing the challenge of calling the componen ...

Using JavaScript to parse JSON data generated by PHP using an echo statement may result in an error, while

I am facing an issue with parsing a JSON string retrieved from PHP when the page loads. It's strange that if I send an AJAX request to the same function, the parsing is successful without any errors. The problem arises when I attempt to do this: JSON ...

Shift div position when clicked and employ jQuery animations

I have been attempting to add animation to a DIV element by using jQuery. The goal is to move the DIV from the center to the left when it is clicked, but I am encountering some issues. The parent div has a position of "relative", and my initial idea was to ...

Invoke a function immediately after the application has finished loading (using both jQuery mobile and PhoneGap)

Currently, I am facing an issue with my application (built with jQuery-mobile and Phonegap). The problem lies in the need to execute a function that makes an ajax call returning a URL of an image. After receiving the JSON response containing the URL, I wa ...

Modify the appearance of HTML dialog box using CSS styling

Currently, I am working on a Java Spring project that includes a single CSS file consisting of around 1500 rows. Recently, I was tasked with adding a dialog box to one of the screens in the project. The issue I encountered is that the style of the dialog b ...

Leveraging AJAX requests for database connectivity in CodeIgniter

I have encountered an issue with a script (applications/view/pages/home.php) that utilizes an AJAX request to fetch and display the content of another script (referred to as scheduler.php). The scheduler file contains dynamically modified HTML based on a p ...

Alternative Options for Playing Audio in Internet Explorer 8

I'm in the process of setting up a button that can both play and pause audio with a simple click. While this functionality is working smoothly in modern browsers like Google Chrome, I am facing compatibility issues with IE 8. Even after attempting to ...

Once the hidden DIV is revealed, the Youtube video within it begins to load

I currently have a hidden DIV on my website that contains a YouTube clip. My goal is to load the video in the background once the page has fully loaded, so that it's ready to play when the user clicks the button to reveal the DIV. However, at the mo ...

The system encountered an error when attempting to convert the data '19-10-2002' into a date format

I am trying to pass a date parameter in the format (dd-MM-yyyy) and then convert it into the format (yyyy-MM-dd) before sending it via API. Here is my code: convert(date:string){ date //is in the format(dd-MM-yyyy) date = formatDate(date , " ...

Updating the total of textboxes using jQuery

I'm working on a jQuery function that totals the values of 7 textboxes and displays the result in a grand total textbox: $(document).ready(function() { $('.class2').keyup(function() { var sum = 0; $('.class2').each(funct ...

Having issues with the input event not triggering when the value is modified using jQuery's val() or JavaScript

When a value of an input field is changed programmatically, the expected input and change events do not trigger. Here's an example scenario: var $input = $('#myinput'); $input.on('input', function() { // Perform this action w ...

How can I make an absolutely positioned div slide down from a specific area on the page using jQuery's slideDown() function?

I have a website project in progress where the main image on the homepage is of a house with windows and a door. The idea is that when a user clicks on a window, a <div> should appear with some text. Similarly, clicking on the door should trigger a & ...

PHP and AJAX can sometimes cause variable values to become lost

I've been encountering an issue while attempting to validate an email using ajax. For some reason, the mail variable seems to disappear during the process. Despite thoroughly checking for typos multiple times, I can't seem to locate any errors. T ...

Escaping double quotes in dynamic string content in Javascript can prevent unexpected identifier errors

Need help with binding the login user's name from a portal to a JavaScript variable. The user's name sometimes contains either single or double quotes. I am facing an issue where my JavaScript code is unable to read strings with double quotes. ...

Utilizing Images with 'General Drawing' in Highcharts

I'm currently attempting to create a task diagram using Highcharts. I had the idea of incorporating images using the <img> tag ren.label('<img src="/images/test.jepg', 10, 82) .attr({ ...

The contrast in timing between server runtime and client waiting time during Ajax calls is substantial

Two REST endpoints are utilized to drive navigation on a website. While both generate almost identical responses, one retrieves its data directly from the database, while the other must first query a search engine (solr) for some data before making databas ...