Using the CSS to set the alt attribute and title of an image based on its file name

Is it feasible to dynamically set the ALT and title attributes of an image element to match the file name? It seems like this could potentially be achieved using the CSS content property, although I am not very familiar with the process.

For example:

<img src="file_name.jpg" alt="File Name" title="File Name">

If this is indeed possible, I would also like both attributes to have clean and formatted values without any underscores. Perhaps this can be accomplished through CSS as well?

Answer №1

$('img').attr({ //modify multiple attributes
    title: $(this).attr('src').replace('_', '').replace('.jpg', ''), //remove _ and .jpg from src attr
    alt: $(this).attr('src').replace('_', '').replace('.jpg', '')
});

Using content is restricted because:

CSS contains a property named content that is exclusively used with the pseudo elements :after and :before. Although written like a pseudo selector (with the colon), it's actually a pseudo element as it does not target any existing elements on the page but appends additional content to it.

While this can insert text before or after an element, it doesn't alter its properties!

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

Block entry to HTML files unless the user has logged in utilizing PHP sessions

I have been working on implementing a verification process to check if a user is logged in when accessing a specific HTML file. When a user tries to access something.html without being logged in, I would like to redirect them to index.php and restrict acc ...

Transferring SQL server dates to jQuery Calendar through AJAX communication

I am currently working on implementing a jQuery calendar example, and I want to load the dates from my SQL database instead of using hardcoded values. I am considering using Ajax post to send a request to my web method and retrieve the data. However, I am ...

Implementing automatic redirection upon clicking using React without the need for manual clicking

I'm experiencing an issue where the page seems to automatically navigate to another page without clicking on the div. Can anyone explain why this is happening? Here's the code snippet for reference: import React, { Component } from "react&q ...

The essence of my design disappears when I generate a canvas object using Fabric.js

After defining top and left attributes for a Canvas Object using a JavaScript function, I encounter an issue when creating a fabric Canvas object. var fabricCanvas = new fabric.Canvas('mycanvas'); Upon creation, my HTML Canvas has its top and ...

Is it possible to combine several m3u8 files into a single m3u8 file?

I am looking to consolidate multiple m3u8 files into a single file for seamless playback in one video player. How can I achieve this without compromising the individual clips? For instance, if I have zebra.m3u8, giraffe.m3u8, and lion.m3u8 files, is it pos ...

Adjust the source of the HTML5 video tag based on the orientation of an iPad

I am currently coding an HTML5 page specifically for iPad Mobile Safari, which includes a video tag for embedding video files. I have successfully implemented CSS3 media queries to determine the orientation of the iPad. My next challenge is to dynamically ...

The dynamic filtering power of jQuery LiveFilter seamlessly integrates with a category filtering

Hello, I have a question about utilizing liveFilter along with a category filter. Currently, the code filters by typing OR category selection, but I would like to implement a search function that filters by BOTH. For example, if the 'action' cate ...

Ways to format the direct descendant of a multi-level list?

Check out my nested ul list: <ul> <li> <a href="/"></a> </li> <li> <a href="/"> </a> &l ...

CSS: The Ultimate Guide to Concealing the Second Row in a Table

I have a single table in my code: <table> <tr><td>sample data 1</td></tr> <tr><td>sample data 2</td></tr> <tr><td>sample data 3</td></tr> <tr><td>sample data 4</t ...

jQuery scripts fail to load upon returning using the back button

Attempting to create a website similar to GitHub using PJAX. The page functions normally when utilizing PJAX links, but encounters issues when the back button is clicked. While the content loads successfully, the jQuery scripts are not ready. To see this ...

jQuery has a problem with the $.each method

I've created a function that checks elements with the class name .sitem. If any of these elements have a blank value, it should return an error message. Otherwise, the code should proceed as normal. if($(".sitem").each(function(ss, element) { if($(t ...

The CSS transition seems to be malfunctioning

I need help figuring out how to make the submenu in the navigation bar appear with a transition effect. The CSS code I'm currently using is: #navigation li ul { display: none; width: auto; position: absolute; margin: 0; padding: 0; ...

The Jquery validation in Asp.Net MVC 5 does not seem to work properly when using ajax post during a submit event, as it incorrectly

I've double-checked all the necessary elements for client-side validation, but unfortunately, the validation is still not working. I would greatly appreciate it if someone could assist me in identifying what I might have overlooked or where I may hav ...

Encountered an unforeseen token "<" that is causing the server to return a 404 HTML page instead of the intended .js

I am currently developing a to-do list application using Node.js Express and EJS. In the app, I have implemented a filtering functionality with a URL path of "/filter/query". Based on the selected category (either "lists" or "lastupdated"), the app filters ...

The jQuery selector seems to be ignoring elements within a different scope

I'm attempting to perform an ajax request to a specific page on my website using this particular element as a direct child of the body tag: <div class="container" id="wantme"><div class="content"></div></div> There is only on ...

The non-disappearing nature of -webkit-appearance

I have been attempting to eliminate the -webkit-appearance:none; property using jQuery, but all my efforts have been unsuccessful. Here are the methods I have tried: jQuery(document).ready(function(){ jQuery("select").removeAttr('style'); j ...

A guide on sorting JSON data with Javascript/jquery

I have a set of JSON data: {"X1":"3","X2":"34","Y1":"23","Y2":"23","Z1":"234","Z2":"43",...} My goal is to rearrange and group this data as follows: var newDataJson1 = { "X":{"X1":"3","X2":34}, "Y":{"Y1":"23","Y2":"23"}, ... } ALSO, I want to stru ...

Error in jQuery: the variable has not been defined

I am currently working on creating a custom plugin using the code below. I have encountered an error at the line if(options.controls == true) The specific error message says 'options is not defined'. How can I properly define this variable? ( ...

Ways to elegantly re-establish a websocket connection?

I've been working on a single-page application that utilizes HTTP and Websockets. When the user submits a form, I start streaming a response back to the client. Here's a snippet of the client-side code: var html = `<!DOCTYPE html> <met ...

When using Owl Carousel in Chrome, the carousel unexpectedly stops after switching tabs

Hi there, I'm currently testing out the auto play feature in owl carousel. One issue I've encountered is that when I switch to another tab in Chrome and then return to my webpage with the carousel, it stops functioning unless I manually drag the ...