Navigating the style properties using jQuery loops

I have an event listener for a click on a specific div.

$('div').on('click', function() {
    var styles = $(this).attr('style').split(';');
    $.each(styles, function(i) {
        var style = this.split(':');
        alert(style[0]);
        if(style[0] == 'font-size'){
            $('#controls #font-size option[value="'+style[1]+'"]').attr('selected', true);
        } else if(style[0] == 'color'){
            $('#controls #color option[value="'+style[1]+'"]').attr('selected', true);
        } else if(style[0] == 'text-align'){
            $('#controls #text-align option[value="'+style[1]+'"]').attr('selected', true);
        }
    });
});

The alert() will display the style property (e.g. font-size, color, width), but the if statement is not functioning as expected.

Any advice on how to fix this?

Answer №1

It is likely that there exists white space within your style declaration. Make sure to check if the following condition holds true:

$.trim(style[0]) == 'font-size'

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

Executing the JavaScript function only a single time is what you're looking

I'm having trouble getting this script to work correctly on my website. It is supposed to trigger an alert when the specified id is in the viewport, but the issue I am encountering is that the alert keeps popping up repeatedly while the id is still vi ...

Guide on looping through deeply nested children within an object to accumulate a list of names

Within an object, there are numerous parent and child elements var obj={ name: 'one', child:{ name: 'two', child:{ name: 'three', child.. } } } foo(obj) Create a ...

Learn the best practices for integrating the options API with the Composition API in Vue3

Using vue3 and vite2 Below is a simple code snippet. The expected behavior is that when the button is clicked, the reactive 'msg' variable should change. It works as expected in development using Vite, but after building for production (Vi ...

Is verifying email and password with jquery possible?

I am currently working on a jQuery form validation project: While the password and username validation are working fine, I am facing issues with email and password confirmation validations. Surprisingly, I have used the same technique for both. If you wa ...

Dynamic page url redirection involves creating search-engine friendly URLs for dynamic

After successfully incorporating URL rewriting into my PHP website, I am facing an issue with displaying the links as desired. mydomain.com/komet-india/Best-Indian-Hill-Stations-1/Delhis-Hotel The elements Best-Indian-Hill-Stations,1,Delhis-Hotel in the ...

Engaging Floor Layout

Currently, I am in the process of developing an interactive floorplan for an office project. The main objective is to provide a user-friendly interface where individuals can search for a specific employee and have their respective office location highlight ...

Executing Parent function in Reactjs (Triggering Drawer when menu item is clicked using Material-ui)

I'm having some trouble trying to activate the drawer when a user clicks on a menu item. Unfortunately, my current solution is not working as expected. Can anyone provide assistance? Parent import React, { Component } from 'react'; // Impo ...

Can anyone recommend any quality libraries for uploading files in Silverlight or HTML that also support metadata?

I am in the process of developing a solution within SharePoint 2010 that allows for the upload of multiple documents while also tagging them with metadata. Is there anyone familiar with an innovative file-upload control or solution that enables the additi ...

On certain websites, the footer may appear distorted or incorrectly displayed when viewed in the IE 9 web browser

Currently experiencing an issue with the display of the footer in IE9. On some pages, the footer is not properly fixed and overlaps the middle of the content. However, this problem does not occur when using other web browsers. Here is the code that I am c ...

Using Object Value as Variable instead of String in JavaScript/React

When working with React & JavaScript, I am storing an input name as a string in an object: window.ObjectOne = { ObjectOneKey: "MyInputName", } The input name can be an object path like "Object.FirstName" or a variable name "MyVariableName" What I am at ...

What is the best way to verify an email address in Vue.js?

Issue with email validation using regex in Vue.js <div class="input-wrapper"> <div class="email-icon"></div> <input type="email" v-model.trim="$v.email.$model" v-validate="'required'" :class="{ 'is-invalid': v ...

Issue with opening image modal when clicking on images, JavaScript seems to be malfunctioning

I created a gallery of modal images that I want to open when clicked, but for some reason it's not working as expected. What I'm trying to achieve is passing the image ID of each image to JavaScript when they are clicked so that the script can th ...

The background-color value specified for the get-function rgba-css-var is invalid and causing an error

I have completed the necessary steps to integrate Bootstrap 5 SASS into my ASPNET Core project. To begin, I right-clicked on the wwwroot directory and selected Add->Client-Side Library. From there, I chose the unpkg provider and added the <a href="/ ...

The JavaScript function getSelection is malfunctioning, whereas getElementById is functioning perfectly

I am encountering a peculiar situation while trying to input text into a textbox using a JavaScript command. The CSS locator does not seem to update the text, whereas the ID locator is able to do so. URL: Below are screenshots of the browser console disp ...

Issue with Jquery UI Popup in Internet Explorer 7 and 8

I am experiencing an issue with Jquery UI dialog. Even though I set it to modal:true, the grey overlay appears but the dialog itself does not show up. What's even more puzzling is that when I set autoOpen: true, the dialog displays correctly at first ...

Locate a deeply nested element within an array of objects using a specific string identifier

Trying to search for an object in an array with a matching value as a string can be achieved with the following code snippet. Is there an alternative method to optimize this process without utilizing map? Code: const arr = [{ label: 'A', ...

jQuery disabling a button issue in Internet Explorer 9

I've encountered a issue with my code snippet that disables the upload button until something is selected to be uploaded. The problem I'm facing is that it's not working in IE9 Beta. Do I need to tweak my code specifically for IE? Here' ...

"Revamping Your Design: The Power of Angular 4

I am working with two different layouts in my project: <div *ngIf="!loginPanel" class="login1"> <a (click)="showLoginPanel()">Login</a> </div> <div *ngIf="loginPanel" class="login2"> <input type="text" placeholder="user ...

Is it valid JSON to have values such as "ok", false, true, null, and 123?

Can the following strings be considered valid JSON? "ok" false true null 123 If not, why does the standard JavaScript JSON.parse method allow these values to be used as valid JSON? I have encountered issues when using these values in JSON REST APIs, ...

Update the variable in a PHP script and execute the function once more without the need to refresh the page

Hey there, I'm wondering if AJAX is necessary for the functionality I want to implement. Here's the scenario: I have a function that generates a calendar and I plan to add 'forward' and 'backward' arrows so users can navigate ...