How to call multiple elements by class and .innerHTML? [duplicate] - javascript

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 6 years ago.
I am having problems calling multiple elements with the same class name when using .innerhtml. For instance the url I am working with is:
https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=hen
and when I enter the following code in chrome console I get:
document.getElementsByClassName('a-size-small a-color-secondary')
<span class="a-size-small a-color-secondary">by </span>
<span class="a-size-small a-color-secondary">Adore Plush Company</span>
<span class="a-size-small a-color-secondary">Get it by <span class="a-color-success a-text-bold">Tuesday, Jun 21</span></span>
...
...
But when I enter: document.getElementsByClassName('a-size-small a-color-secondary').innerHTML
I get "undefined". The goal is to get just the text between the span tags.
Can someone give me some guidance? I have tried with different variations including var and even Selection method. It worked with Selection for the first class element but didn't work at all with SelectionAll. Thanks for any help.

document.getElementsByClassName('a') will give the Object with class name .
for example class name is a and there are many spans with a , then you need get the span value by using index of that object
var x =document.getElementsByClassName('a');
console.log("value of spans"+x[0].innerHTML);
COdepen-http://codepen.io/pen/

Related

Dynamic variables with dynamic Event Listener [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(8 answers)
Attach event to dynamic elements in javascript
(15 answers)
Closed 3 months ago.
I have being fighting with this issue for a couple of days now, I'm using eval() to declare some variables that come from firebase. I want to make a clickable url that might allow me to select between different files of the DB.
function Versiones(doc) {
Layout_02.innerHTML += `
<p id="${doc.id}"> Id= ${doc.id} </p>
<p> OT= ${doc.data().OT} </p>
<p> Entrega= ${((doc.data().Entrega).toDate()).toDateString()} </p>
<p> Registrado= ${((doc.data().Generada).toDate()).toDateString()} </p> `;
eval(`const ${doc.id} = document.getElementById("${doc.id}");`);
eval(`${doc.id}.addEventListener("click", ()=>{Documento(${doc.id})});`);
}
function Documento(doc) {
console.log("Recibido: ");
console.log(doc.id);
}
The thing is that, just the last paragraph actually have a click event associated, the other paragraphs doesn't have. Can you help me understand what is happening ?
I have tried changing the variable type, and using onclick event. but nothing works.
Thanks.

get div Tag value [duplicate]

This question already has answers here:
jQuery Event : Detect changes to the html/text of a div
(13 answers)
Trigger for span text/html on changed
(6 answers)
Closed last year.
I really dont know how get the div tag in between value Which in this case is
F064
I have this Hmtl code..
<div class="product--price price--unit">
<span class="price--label label--purchase-unit">articlenumber:</span>
F064
</div>
I want to say if the F064 changes then alert what its changed to.
I want something like this in either JS or JQuery
var divTagVal = $("div.product--price");
$(divTagVal).on('change', function () {
alert(divTagVal);
});
So if F064 changes to F065 the it should alert "F065"
but this doesnt work, what am i doing wrong ?

How to get value from children element using javascript [duplicate]

This question already has answers here:
Getting HTML elements by their attribute names
(8 answers)
Closed 3 years ago.
I have html page with elements:
<div id="location-id" class="form-control">
<span class="hub-trigger" data-hub-recordid="28">Germany</span>
</div>
I use var element= document.getElementById('location-id') to get html element.
Please tell me how can i get 28 from the span ?
I have tried to use var id = element.getElementByTagName('data-hub-recordid') with val(), text(), innerText() at the end but it didn't work as i expected.
Try it
document.querySelector('.hub-trigger').getAttribute('data-hub-recordid')
or it
const parent = document.querySelector('#location-id');
const child = parent.querySelector('.hub-trigger');
const recordid = child.getAttribute('data-hub-recordid');

Javascript starts with or contains [duplicate]

This question already has answers here:
jQuery match part of class with hasClass
(6 answers)
jQuery selector for id starts with specific text [duplicate]
(4 answers)
Closed 4 years ago.
Trying to get some help on some JavaScript we are trying to post accross multiple landing pages. We are sending values to hidden fields on a button click 'lp-pom-button-3'.
However, the ID changes depending on the page, but will always have the format 'lp-pom-button-' with the number potentially changing depending on the page. Is there a function similar to 'like' in SQL, or saying any button that starts with or contains 'lp-pom-button-" for this to be triggered on? Thanks for you help!
<script type="text/javascript">
document.getElementById('lp-pom-button-3').addEventListener(
'click', function(event) {
ga(function() {
var tracker = ga.getAll()[0];
var clientId = tracker.get('clientId');
document.getElementById('GACLIENTID').value = clientId;
var userId = tracker.get('userId');
document.getElementById('GAUSERID').value = userId;
});
});
You can use the css selector for attribute value contains.
[id*="lp-pom-button-"]
or attribute value starts with:
[id^="lp-pom-button-"]
In vanilla javascript you can access elements using css selector with querySelector or for getting a list of elements querySelectorAll.
Here is an example:
document.querySelector('[id^="lp-pom-button-"]');
And HERE is a list of all the browsers and browsers version that support this function.

JS how right replace element? [duplicate]

This question already has answers here:
jQuery if div contains this text, replace that part of the text
(5 answers)
Closed 8 years ago.
...
<div>
<div>
{FirstName} {POSITION}
</div>
</div>
...
We would like find element {FirstName} and repalce him on test.
For this we make:
$("{FirstName}").replaceWith("test");
But it is not working...
Tell me please how right make replace?
What you should do is target the parent DIV (lets pretend it has a class of .test)
var content = $('.test').html();
var new_content = content.replace('{Whatever}', 'Hello');
$('.test').html(new_content);
Or in short (I haven't tested this, but it should work)
$('.test').html($('.test').html().replace('{Whatever}', 'Hello'));
If you just want to replace a string in DOM elements, the question has been asked before :
Replace all strings in dom element
But best to do this would be to use a template engine.

Categories