Unable to change font using jquery - issue persisting

I am struggling to modify the font of a specific text in the following code, as it keeps displaying a page error.

 function adjust_font_style(div_id) {   
 $("#testsave").html(<strong onclick=\"javascript:save_font_changes("+div_id+",font,'Arial')\">Done!</strong>
 } 

function save_font_changes(div_id,font_setting,settings_value) {  
      if(font_setting=="font"){ 
          $("#"+div_id).css('font',settings_value);
      }else if(font_setting=="size"){
          $("#"+div_id).css('fontSize', settings_value);
      }else if(font_setting=="colour"){
          $("#"+div_id).css('color',settings_value);
}
 } 

Answer №1

When it comes to syntax highlighting, it is crucial to remember to include quotes when passing a string to .html(). In the following example code snippet:

function edit_font(div_id) {   
  $("#testsave").html("<strong onclick=\"javascript:save_font_stuff("+div_id+",'font','Arial')\">Done!</strong>");
}

If you miss adding quotes around the string in general and specifically around the font variable in the function call, it will result in an error. Perhaps considering using a non-inline event handler would be beneficial. For instance:

function edit_font(div_id) {   
  $("#testsave").empty().append(
    $("<strong>Done!</strong>").click(function() { 
      save_font_stuff(div_id,'font','Arial');
    })
  );
}

You can also streamline the other function or even collapse it, like shown below:

var setting_map = { font: 'font', size: 'fontSize', colour: 'color' };
function save_font_stuff(div_id, setting, value) { 
  $("#"+div_id).css(setting_map[setting], value);
} 

Answer №2

The onclick event handler in your code is failing because it does not pass a string as the second argument. To fix this issue, make sure to wrap the font argument in single quotes to convert it into a string.

function update_font_value(container_id) {   
 $("#update").html('<strong onclick="javascript:save_updated_font(\"'+container_id+'\",\'font\',\'Arial\')">Complete!</strong>');
 } 

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

Implementing AJAX to Access a PHP File Across Multiple Web Pages

I devised a personal framework for myself. I implemented an ajax script on each page of my website by including a single PHP file in every webpage. Here is the script I added to my add.php file: function demo(str) { if (str.length==0){ ...

Why isn't the value sending in the AJAX post request?

I'm new to working with ajax and encountering an issue while trying to send data via ajax post. Even though the output of console.log(obj.Id) and console.log(oke) is 2, when I try to send it through data in ajax, it ends up as 0 in the controller. $ ...

The dropdown feature of the Angular-UI typeahead directive is not functioning

Recently, while using the AngularUI typeahead directive, I encountered an issue where the dropdown appeared cut vertically with a scrollbar on the right. Despite this visual glitch, all the data was displaying correctly within the typeahead box. The image ...

Retrieving PHP form values using JQuery

How can I transfer a form input value from a PHP file to a JavaScript file? Here is the code I am currently using: <html> <head> <meta charset = "UTF-8"> <title> Search Customer by Field </title> <s ...

Is it possible to send POST data to a top-level iframe using Javascript or jQuery when redirecting?

This inquiry is in reference to a question about sending POST data on redirect using Javascript/jQuery. The proposed solution involved creating a hidden form that would be submitted to manage the POST and redirect process. However, I am planning to generat ...

Unusual characteristics found in a select list controlled by Angular

The article titled "How to set the initial selected value of a select element using Angular.JS ng-options & track by" by @Meligy served as my guide while working on implementing a select list (ng-options). Despite achieving the desired basic functional ...

Preventing error messages from impacting the layout of the form in jQuery validate

Is there a way to prevent error messages from shifting elements to the left when they appear in the form? Here is an example ...

What could be causing the video not to display in landscape mode on the iPad Pro?

I'm having an issue with a standard HTML5 <video> on my website. The videos are working fine on most devices, however there is a problem specifically with the iPad Pro in landscape orientation. It seems to work properly in portrait orientation ...

Can you tell me the name or purpose of this particular pattern?

While searching, I didn't come across a post inquiring about a pattern like the one below. My apologies if I overlooked it. Lately, I've noticed this particular pattern frequently used in various jQuery plugins or scripts: (function () { .... ...

The Vuetify rating system seems to be having trouble displaying

I've integrated the Vuetify rating component into my app (https://vuetifyjs.com/en/components/ratings#ratings), but I'm facing an issue. Despite having Vuetify 1.5.5 installed and working with other components like buttons, the stars in the ratin ...

When the draggable item is released near the drop zone, allow drag and drop to combine

In my latest project, I created a fun game that involves matching different shapes. The goal is to drag the correct figure next to its corresponding shadow figure for a perfect match. Currently, if you partially overlap the square with its shadow, the game ...

Tips for transferring data from jQuery to a Spring Controller and receiving a List in return

I have a project in progress that involves passing form parameters like the database name and query to the Spring Controller. The Controller then sends the query to the corresponding service class (based on the dbname) and returns the result set as a java. ...

How does conditional rending of function components in react affect the positioning of my css elements?

I'm in the process of creating a website as part of a project, where I have set up a vertical navigation bar next to the main content area. Utilizing bootstrap, I divided the layout into two main columns - one for the navigation bar and the other for ...

What is the best way to insert an image icon within an input field using bootstrap?

I am currently working on enhancing the design of my input text field by incorporating an image icon using Bootstrap. I have successfully used a font-awesome icon within the input tag as an example. Here is the example of what I have implemented: https:/ ...

Attempting to modify the animation speed of a fadeOut queue in jQuery from "slow" to the digit "3000" is proving unsuccessful

Here is a snippet of code that I have successfully used for animation and fadeOut simultaneously: $('#vote').fadeOut({queue: false, duration: 'slow'}); $('#vote').animate({ top: "-10px", left: "25px" }, 'slow'); Ho ...

Assigning a class to an li element once the page has finished loading

I am facing an issue with my navigation bar where the links are dynamically loaded from a database using a foreach loop. Although the nav bar is static, I want to apply an 'Active' class to the link when it is currently active. Despite trying to ...

Is there a way to gradually reveal an element as I scroll to a specific position?

If the window top position is about 100px from the bottom, I want to create a fading in/out effect for the element overlay. Check out this image Any ideas on how to achieve this? .section-card{ position: relative; height: 500px; line-heigh ...

Create a span in the middle with an ellipsis that seamlessly integrates with an icon on the left

I am looking to incorporate ellipsis into li.text, while still maintaining the presence of the right span (which contains numbers) and the spacing (padding) between the number span and the text span. Is there a way to achieve this? .right { float: ri ...

What is the best way to bring focus to a button within a modal once the modal has been opened

When displaying a modal popup, I want the focus to be on the first button within the modal. This button is added dynamically to the footer of the modal var difVal = ( totalAmnt - payAble ).toFixed(2); $('#chqMsgContent').html( 'T ...

Are there alternative methods to improve the responsiveness of a column layout?

Check out this codesandbox I created to display my current status. When you change the column-count to three, a significant amount of information becomes hidden because it doesn't scroll. I've attempted using grid and flexbox layouts, but everyt ...