Are there any CSS3 selectors currently available or planned for matching partial attribute names?

By using [foo^="bar"], you can target nodes with the attribute foo that starts with the value bar.

Is there a method to select nodes based on an attribute name that begins with a specific string? This would be useful for selecting all nodes with a data-* attribute.

Edit: I am interested in this approach to optimize performance, as it allows me to avoid iterating through all nodes to find these attributes. I plan to use querySelectorAll and its Sizzle polyfill for older browsers.

Answer №1

Another approach is to utilize the .filter() method in jQuery:

$('target').filter(function() {
    return $.grep(this.attributes, function(attr) {
       return attr.nodeName.indexOf('custom') === 0;
    }).length;
});

http://jsfiddle.net/RJLut/

Answer №2

While it may seem like an excessive solution, creating a custom selector could benefit you:

$.expr[':'].attribute = function (element, index, regex_string) {
    var regex = new RegExp(regex_string[3]);

    for (var i = 0; i < element.attributes.length; i++) {
        if (element.attributes[i].name.match(regex)) {
            return true;
        }
    }

    return false;
};

Example: http://jsfiddle.net/RJCTm/

In your specific situation, you would use:

$('div:attribute(^data)') // starts with
$('div:attribute(foo$)')  // ends with

This approach is somewhat reminiscent of the standard attribute selector syntax.

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

(Is it even necessary to use a timezone library for this straightforward scenario?)

As I delve into the realm of time zones for the first time, I've heard tales of how challenging it can be for developers. To ensure I am on the right track, I am posing this question as a safeguard to make sure nothing is overlooked. My scenario is q ...

Angular JS may encounter a cross domain problem when attempting to post on a third-party website

HTML Code: <div ng-app="myApp" ng-controller="myController"> <div> <input type="button" data-ng-click="submit()" ...

What is the best way to incorporate variables into the useState hook in React?

I want to create multiple useState hooks based on the length of the userInputs array. The naming convention should be userInput0, userInput1, userInput2,...userInputN. However, using "const [userInput+index.toString(), setUserInput] = useState('' ...

Angular filter within a nested ng-repeat loop

I've encountered an issue with nested filtering in Angular, where two filters are dependent on each other. What I'm trying to achieve is the following: <div ng-repeat="g in groups | filter:groupFilter"> ... <tr ng-repeat="c in g.co ...

Order of custom code and JQuery in ASP master page

Using an ASP master page to include all the Javascript and jQuery files has presented a challenge for me. Specifically, the jQuery function in OrderManagement.js $(".menu-item").click(function () { window.alert("some text"); }); fails to execute whe ...

SSL certificate not being presented by the Socket.io chat server

I recently discovered that the chat feature in an application I developed a while ago is not working after switching the website from http to https. It seems like I need to SSL my Socket.io chat socket to avoid browser errors. However, when I try to conne ...

Is it possible to create a dynamic image in WordPress and have it automatically set as the background in the Style.css file?

I successfully transformed an HTML template into a WP theme and now I aim to add dynamic elements throughout. My first task is to make the slider (comprised of image, heading, content, button) dynamic. Currently, all the code resides in index.php except ...

What is the best way to shift a link to the right within a "div" element?

I've been working on setting up a navigation menu for my website, and I'm having trouble getting the "quit" link to align to the right while keeping the other links aligned to the left. I've tried various methods but just can't seem to ...

What is the best way to choose "this" element in Angular?

Within my template, I have 5 buttons each utilizing the same ng-click function. These buttons essentially function as a tabbed navigation system, where clicking on one button directs the user to that specific tab's content pane. All of these buttons c ...

What is the average time required for the page to load or respond?

I am curious about the loading and response time of a webpage, specifically how long it takes for a page to load and respond to a given request before timing out. I have noticed that one of my pages is slow to respond and I would like to adjust the durat ...

What is the best way to align two span tags to the right on separate lines?

I am facing an issue with the positioning of three span tags: A is floated left while B and C are floated right. My desired layout is to have C positioned below B, both floating right on separate lines. I attempted using display:block for B but it did not ...

Babel failing to transpile source code of npm link/symlink package

I am in the process of establishing a shared module environment using node. Below is an outline of my directory structure: project |--common | |--package.json | |--graphql | |----schema.js | |--server |--package.json |--serv ...

"Mastering the art of debouncing in Angular using

I am facing an issue where, during a slow internet connection, users can press the save button multiple times resulting in saving multiple sets of data. This problem doesn't occur when working locally, but it does happen on our staging environment. E ...

Utilizing regular expressions in Javascript to retrieve multiple instances

This paragraph contains a specific string txt = "Local residents o1__have called g__in o22__with reports..."; that requires extracting numbers between each occurrence of o and __ If we use the following regex: txt.match(/o([0-9]+)__/g); We will obtain ...

What is the best way to implement CSS for text within a text area?

Is there a way to make the tags dragged from the dropdown list to a text-area non-editable? Currently, the text in the text-area is editable as shown in the GIF. //TextArea HTML Helper @Html.TextAreaFor(m => m.Formula, new { @class = "form-cont ...

What could be causing the datepicker to not acknowledge any selected options?

I'm currently facing an issue where I am trying to populate a select dropdown with available hours that do not have appointments booked. The problem seems to be related to the datepicker plugin behavior. I want the dropdown to be populated based on th ...

How can I utilize JQuery to dynamically refresh a dropdown menu?

My dropdown list is initially empty: <div> <label>Boarding Point </label> <select title="Select pickup city" id="boardingDropdown"> </select> </div> I am trying to popula ...

How can I dynamically alter the color of a table row without using _rowVariant?

I am utilizing a bootstrap-vue table to showcase information retrieved from a JSON file. One piece of information I receive is an integer labeled "Status", and I aim to adjust the row's color based on this variable. For instance, if the Status equals ...

Assign a class to the "li" element containing an "a" tag with the specified href attribute

Is it possible to transform the "href" attribute of an element into a class or id like "li" for better css handling? <ul> <li><a href="#section3"><span class="fp-sr-only">due</span><span></span></a><d ...

Am I utilizing the htmlspecialchars function properly?

My main objective is to protect the user from malicious code and prevent XSS attacks. I have implemented a series of checks to filter the user's input before storing it in the database. The user input is stored in the $post variable. $post = htmlspec ...