gpt-4-0314· openai
Suzhen/CodeChat
Can you write me some js code that does the following? - go through each element that…
- User
Can you write me some js code that does the following?
- go through each element that has the class "aditem-main--middle":
- with the element, extract the number of a child element with alphanumeric content with the class "aditem-main--middle--price-shipping" as a variable.
- with the element, extract the number of a child element with alphanumeric content with the class "aditem-main--bottom" as a variable.
- with the element, append some text to the cild element "aditem-main--middle--price-shipping".
- go through each element that has the class "aditem-main--middle":
- Assistant
Here's the JavaScript code that does what you requested:
// Get all elements with the class 'aditem-main--middle' const elements = document.querySelectorAll('.aditem-main--middle'); // Iterate through the elements elements.forEach(element => { // Extract the number from the 'aditem-main--middle--price-shipping' child element const priceShippingElement = element.querySelector('.aditem-main--middle--price-shipping'); const priceShipping = parseInt(priceShippingElement.textContent.match(/\d+/), 10); // Extract the number from the 'aditem-main--bottom' child element const bottomElement = element.querySelector('.aditem-main--bottom'); const bottomNumber = parseInt(bottomElement.textContent.match(/\d+/), 10); // Append some text to the 'aditem-main--middle--price-shipping' child element priceShippingElement.textContent += " (appended text)"; });This script will go through each element with the class "aditem-main--middle" and perform the specified actions, such as extracting numbers from child elements and appending text to the "aditem-main--middle--price-shipping" child element.