Retrieving all buttons from a webpage and removing those with a specific background-image using JavaScript

Hey everyone, I'm still learning about javascript and other web development languages.

My current task is to locate all the buttons on a webpage and remove the ones that have a specific background image.

I know about using getElementsByTagName but need guidance on what to do next.

So far, I have this code snippet:

document.getElementsByTagName(button)

Could someone please advise me on the next steps?

Thank you for your assistance.

Answer №1

Just follow these simple steps :

  1. Collect all elements
  2. Inspect each element to find a specific background-image
  3. Delete the element if it matches

This is the HTML code you need :

<div id="parentNode">
    <button style="background-image:url('http://i.dailymail.co.uk/i/pix/2013/11/11/article-2500617-007F32C500000258-970_306x423.jpg')">First</button>
    <button style="background-image:url('http://www.allgraphics123.com/ag/01/14142/14142.gif')">Second</button>
    <button style="background-image:url('http://jitendravaswani.files.wordpress.com/2013/02/191356xcitefun-cartoon-pluto.jpg')">Third</button>
    <button style="background-image:url('https://lh5.googleusercontent.com/-WsZ4Q7Y156A/AAAAAAAAAAI/AAAAAAAAABM/vN1-gy0xZQU/photo.jpg')">Fourth</button>
</div>

Here's the javaScript code to make it work:

var imageButtons = document.getElementsByTagName("button");
var parent = document.getElementById("parentNode");
for(var index = 0; index < imageButtons.length; index++ ){

    if(imageButtons[index].style.backgroundImage == "url(http://www.allgraphics123.com/ag/01/14142/14142.gif)"){
        alert();
        parent.removeChild(imageButtons[index]);
    }
}

You can also view and test this code on jsFiddle

Answer №2

Give this a shot

<script type="text/javascript">
        var icons = document.getElementsByTagName('img');
        for (var j = 0; j < icons.length; j++) {
            if (icons[j].style.backgroundImage == "specific image") {
                icons[j].style.display = 'none';
            }            
        }
    </script>

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

Explore the capabilities of Chart JS integration using Python Selenium

I've been attempting to click the buttons on this Chart JS located on the webpage . Despite trying by xpath, full xpath, and JS path, I have not been successful. An example of my attempt to press the "All" button can be seen below: Thank you! from se ...

How can I design a search box similar to the one on the android.com website?

Is it possible to replicate the search box effect on the Android website, where the menu items fade out and the search box expands upon clicking the search icon?view image here See the difference before and after clicking the search icon ...

When CSS animations are used on numerous elements, it can lead to variations in the speed of

I made an animation to move elements from the top to the bottom of a page. I have 4 objects with this animation applied, but for some reason, they are moving at different speeds. It's confusing me. What could be causing this inconsistency? body { ...

Setting up authorization levels for roles in Discord.js

Hi everyone, I came across this script that deals with users posting invite links. How can I whitelist specific channels to prevent the bot from banning or kicking users for posting invite links? Any help would be greatly appreciated. Thank you. adminCli ...

What is the best way to format the information when using response.send() in express.js?

I need help with customizing the content I'm returning in the app.post() method. Is there a way to do this? Below is an example of the app.post() code: app.post("/",function(req,res){ const query = req.body.cityName; const cityName = query.charA ...

Guide to changing the checkbox value using JavaScript

Describing the Parent Element: <span style="background: yellow; padding: 50px;" onClick="setCheckBox()"> <span> </span> <input type="checkbox" name="f01" value="100"> </span> ...

Assigning a value to a variable from a method in Vue: a step-by-step guide

I'm having trouble assigning values from a method to variables in HTML. Here's what I have in my code: <b-card-text>X position {{xpos}}</b-card-text> <b-card-text>Y position {{ypos}}</b-card-text> I would like to assign v ...

Express npm dependency fails to start globally

I have recently reinstalled my operating system from Windows 8.1 to Windows 8.1, and I have been using npm for quite some time. Previously, it was working fine as mentioned here. After the reinstallation, I tried installing npm i -g express, but it does n ...

Using the Github API in javascript, store an image as an image by saving its base64 Data URL

I have a base64 encoded Data URL of a webp image that looks like this: data:image/webp;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs= My goal is to push this image to GitHub using their API in the form of a webp image. To clarify, I want to achi ...

PHP encountering a bad escaped character while parsing JSON using JSON.parse

I'm encountering an issue with JSON parsing. In my PHP code, I have the following: json_encode(getTeams(),JSON_HEX_APOS); This returns a large amount of data. Sample data: To provide more clarity, let's assume I have this: my_encoded_data ...

Explore flat data querying using Firebase and AngularFire

I am working with data stored in firebase, utilizing a flat structure with key-value pairs to establish a many-to-many relationship between events and users (see figure 1). While it's straightforward to look up events associated with a user using pure ...

Attempting to compare the HTML datetime with the current time in order to implement conditional formatting

I am working with a SharePoint discussion list that includes a field named "Last Updated." My goal is to identify and highlight rows where the last update was more than 1 hour ago. Here is my current approach: <html> <head> <sc ...

Mobile devices not responding to media queries properly

My website has different sets of CSS styles for various screen sizes: (1) @media only screen and (max-width: 566px) { (2) @media only screen and (min-width: 567px) and (max-width: 767px) { (3) @media only screen and (min-width: 768px) and (max-width: 999 ...

Craft a CSS triangle shape from scratch

Looking at the image of a blue triangle, I am curious about how to recreate it using CSS. Would using a background image be the best approach, or is there a way to achieve this with CSS transforms? I noticed the triangle is on the left side but not on the ...

Tips for Customizing Dialogs with CSS Classes in mui5 Using Emotion/Styled

When attempting to customize the styling of a mui Dialog, I encountered an issue where it wouldn't accept className even when placed inside PaperProps. While inline styles worked, my preference was to import styles from a separate stylesheet named Sty ...

"Learn how to dynamically update the user interface in express.js using handlebars without having to refresh the

As a newcomer to using express.js and Handlebars, I am faced with the challenge of implementing autocomplete functionality. Specifically, I want to enable autocompletion in a text input field without triggering a page refresh when users interact with it. C ...

Utilize the data storage API within Next.js or directly in the user's

Struggling to store this ini file on either the server or client, any help would be greatly appreciated. Additionally, I would like to display the ini info in the browser so that clients can easily copy and paste the information. However, I seem to be fac ...

Running concurrently, the JavaScript if and else statements execute together

I am looking to create a clickable link that changes the background color when clicked. If the same link is clicked again, I want the background to return to its original state. Here is my current script: <div style="background: transparent;" onclick=" ...

Showing JSON data using CSS and HTML

Hello there! I've come across a coding hurdle today. I attempted to retrieve JSON data from a URL using the following PHP code snippet: <?php $url = "http://services.faa.gov/airport/status/LAX?format=json"; $ch = curl_init(); curl_setopt($ch,CURL ...

Changing the main domain of links with a specific class attribute during an onmousedown event - is it possible?

We are facing a situation where we have numerous webpages on our blog that contain links from one domain (domain1.com) to another domain (domain2.com). In order to avoid manual changes, we are attempting to achieve this without altering the link (href). Th ...