Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage.

Here is the JavaScript code I am using:

(function($) {
$.fn.changeDiv = function( options ) {

    var settings = $.extend({
        // These are the defaults.
        text  : 'Hello, World!',
        color: "#556b2f",
        backgroundColor: "white",
        borderColor : "white",
        borderWeight: "1px;",
        fontWeight   : 'bold',
        fontStyle   : 'Trebuchet MS, Callibri, Sans-Serif',
        fontSize  : '18px',
        position: "center",
        fl : "auto",
        wt: "auto",
        ht: "auto",
        mRight: "auto",
        mLeft: "auto",
        complete : null
    }, options );
            return this.each( function() {

         $(this).text( settings.text );

        if ( settings.color ) {
            $(this).css( 'color', settings.color );
        }

        if ( settings.fontStyle ) {
            $(this).css( 'font-style', settings.fontStyle );
        }

        if ( settings.fontWeight ) {
            $(this).css( 'font-weight', settings.fontWeight );
        }

        if ( settings.fontSize ) {
            $(this).css( 'font-size', settings.fontSize );
        }

        if ( settings.wt ) {
            $(this).css( 'width', settings.wt );
        }

        if ( settings.ht ) {
            $(this).css( 'height', settings.ht );
        }

        if ( settings.position ) {
            $(this).css( 'text-align', settings.position );
        }

        if ( settings.fl ) {
            $(this).css( 'float', settings.fl );
        }

        if ( settings.mRight ) {
            $(this).css( 'margin-right', settings.mRight );
        }

        if ( settings.mLeft ) {
            $(this).css( 'margin-left', settings.mLeft );
        }

        if ( $.isFunction( settings.complete ) ) {
            settings.complete.call( this );
        }
  });
};

In my function call, I use this method:

$('#header').changeDiv({
text: 'This is the header div',
    fontStyle: 'Verdana, Arial, Helvetica, Sans-serif',
fontWeight: 'bold',
backgroundColor: '#406481',
fontSize: '30px',
ht: "50px",
position: "center",
complete    : function() { $(this).easeIn("slow",1500); }
});

The outcome only shows the text 'This is the header div' in green color and centered alignment. However, when I apply the same changeDiv function to the menu, no changes occur. Upon altering the return function like so:

return this.css({
    color: settings.color,
    backgroundColor: settings.backgroundColor,
    borderColor: settings.backgroundColor,
    borderWeight: settings.borderWeight,
fontWeight: settings.fontWeight,
fontStyle: settings.fontStyle,
fontSize: settings.fontSize,
position: settings.position,
fl: settings.fl,
wt: settings.wt,
ht: settings.ht,
mRight:settings.mRight,
mLeft: settings.mLeft
});

I can observe through FireBug that the styles are indeed being applied to the divs, however, the default text remains unchanged.

Seeking assistance!

Answer №1

Ensure to incorporate the $.fn.changeDiv function within your code.



    $(function($) {

Remember to include the $ character at the start of your first line when referencing the changeDiv function. Also, make sure to use this specific definition when calling it. This approach has proven successful for me in my jsFiddle project.

Answer №2

It seems like the key to solving this issue lies within the css that you are attempting to write. Ensure that you use font-family instead of fontStyle.

Give this a try:

$(this).css( 'font-family', settings.fontStyle );

Update

This problem has now been resolved: here is a functional example for others seeking a solution:

Added $ in line 1 and made some changes to the original code

http://jsfiddle.net/e5HHc/1/

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

Download multiple Highcharts graphs on a single page

When using Highchart Export, I am currently able to download multiple graphs in a single page PDF. However, I would like the first graph to be on the first page and the second graph on the second page when saving as a PDF. You can find the code in the fol ...

Nextjs - resolving the issue of shopping carts displaying incorrect total amounts

I am currently facing an issue with the total cart amount while building a shopping cart. The problem arises when I visit the cart page as it only displays the amount of the last item added to the cart. state = { cart: { items: [], total: 0 }, }; ad ...

javascript issue with attribute manipulation

My current struggle involves setting the attribute of an element through programming, but I keep encountering an error in Firebug: obj.setAttribute is not a function. Since I am working with jQuery, allow me to provide some additional code for better conte ...

I keep encountering a 'Missing Permissions' issue whenever I attempt to execute the ban command in Discord.js. What could be causing this problem?

While working on coding a ban command for my Discord server, I encountered an issue where the command was not working as expected. Upon testing, I received an error message on the console stating DiscordAPIError[50013]: Missing Permissions. This was puzzli ...

Tips for combining all included files into one with Babel

My current project involves the use of Babel. Within my server.js file, I have the following line of code: import schema from "./data/schema"; The issue arises because data/schema.js is written in ES2015 syntax. After attempting to compile my server.js ...

What is the best way to fetch d3 data from a service?

I've been exploring a tutorial on creating charts with d3, which can be found at: When it comes to loading data for use in d3, I typically rely on the following code snippet: d3.tsv("data.tsv", type, function(error, data) { The file "data.tsv" is c ...

jQuery struggles to process large response

I am currently developing an application that sends requests to a URL and parses the table found in the HTML response using jQuery. While this method works well for smaller amounts of HTML code, it runs into issues with larger datasets. The problem arises ...

Recursively toggle child elements using JQuery

My knowledge of JQuery is limited, so here's the issue I'm facing. Below is an example of the HTML structure: <ul class="first-class"> <li class="second-class"> <a href="#_"> <i class="fa fa-plus"></i& ...

I am wondering if it is feasible for a POST route to invoke another POST route and retrieve the response ('res') from the second POST in Express using Node.js

Currently, I have a POST route that triggers a function: router.route('/generateSeed').post(function(req,res){ generate_seed(res) }); UPDATE: Here is the genrate_seed() function function generate_seed(res) { var new_seed = lightwallet. ...

Can you explain what findDOMNode is and why it is no longer supported in StrictMode within the console?

I attempted to create a count-up feature using React visibility sensor and React count up, but encountered an error in the console. Is there a correct solution to this issue? Caution: The use of findDOMNode is deprecated in StrictMode. This method was uti ...

Top Method for Initiating AJAX Request on WebForms Page

Looking for the best way to execute an AJAX call in a WebForms application? While ASP.NET AJAX components are an option, some may find them a bit heavy compared to the cleaner approach in MVC. Page Methods can be used, but they require static methods ...

Is it possible to create a Facebook reveal tab using either Javascript or .NET?

As a developer who jumped into Facebook development just before the recent changes, I am feeling lost when it comes to building apps now. I see many questions similar to mine about creating fan-gating features using Javascript only. Is there an up-to-date ...

Is the button not aligned properly with the email input field?

<div class="subscribe__form--action--btn"> <form action="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32545b5e5b5746545C5B5356477F54585850"> </a>" meth ...

Implementing multiple modules within a shared parent route in Angular

Currently, I am seeking a method to load multiple modules under the same path in Angular. Let's say I have three modules: AModule, BModule, and CModule, each with its own RouterModule.forChild call. My goal is to combine these modules under the route ...

Display the final row by hovering over the line

<table> <thead> <tr> <th>First</th> <th>Second</th> <th></th> </tr> </thead> <tbody> <tr> <td>Data1</td> <td>Dat ...

The error message "email() is not a valid function when using the onclick attribute

Can anyone lend a hand? I feel like I must be overlooking something really obvious. I'm having trouble calling my function to execute my ajax call. Any assistance would be greatly appreciated. Thank you! Here is an excerpt of the HTML code: $(docu ...

Incorporating a specific time to a JavaScript date object

In my Angular application, I am facing an issue with adding a time to a date. The appointmentDate from the date picker returns a JavaScript date while the appointmentTime is selected from a dropdown with options like "8:00", "8:30", "9:00", etc. I'm ...

Enhance the dimensions of images on Tumblr

Is there a way to customize the width of a photo in HTML? I am having trouble changing the size of a photo set on Tumblr to be larger. I have tried researching it, but I don't understand where to place maxwidth:600px or if this is even the correct app ...

Ways to modify Angular pipe without specifying data types

I am in the process of extracting an Angular pipe from an application to a library. The current pipe is tied to specific types, but I want to change it to use generic types while maintaining type safety. Original Pipe: import { Pipe, PipeTransform } fr ...

JavaScript embedded within LD-JSON format

Is it feasible to run JavaScript code within an ld+json script? Like for instance, "window.location.hostname" <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "WebSite", "url": "http://" + window.location.ho ...