How to get value="x" from html element [duplicate] - javascript

This question already has answers here:
How to get the value of an attribute in Javascript
(3 answers)
Closed 2 years ago.
I have this code:
<div id="progres-bar" class="progres-bar" value="90"></div>
I want to get value from this element where value="90", so the output should be 90 using javascript. I tried to make this with the below way:
document.getElementById("progres-bar").value;
but it didn't worked!

You can use getAttribute() function.
const value = document.getElementById("progres-bar").getAttribute("value");
console.log(value);
<div id="progres-bar" class="progres-bar" value="90"></div>

Related

.next('tag') in plain javascript? [duplicate]

This question already has answers here:
Get next element with specific class of clicked element with pure js
(3 answers)
Closed 4 years ago.
Is it possible to search for the next sibling-element with a specified tag?
.nextElementSibling is not a function so... can't pass any tag with it?
jQuery
$(event.target).parent().next('div').attr('id')
Java Script
event.target.parentElement.nextElementSibling.getAttribute('id');
you should try using querySelectorAll https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
and then simply just looping over the results for your desired result. Please have a look at the following js fiddle
https://jsfiddle.net/c0bdgxtj/12/
var wrapper = document.getElementById("wrapper");
var debugEle = document.getElementById("debug");
var desiredElements = wrapper.querySelectorAll('div[data-test="y"]');

Javascript - Remove certain character in an element [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 6 years ago.
If I have a input box with value 'jklmnop', how would I go about removing 'k' using javascript?
I was thinking something along the lines of:
<input type='text' id='letters' value='jklmnop'/>
<button onclick='removeK()'>Remove</button
Using this javascript:
function removeK() {
document.getElementById('letters').value.replace('k','');
}
Can anyone tell me why this doesn't work? And if so, what would I need to do to make it work?
Thanks :)
You aren't setting the value.
var l = document.getElementById('letters');
l.value = l.value.replace('k','');

print/alert array in javascript [duplicate]

This question already has answers here:
How can I display a JavaScript object?
(40 answers)
Closed 7 years ago.
I have an array in javascript and I want to print/alert it (without change array).
my code is like this:
function myFunction() {
var fruits = [{'a':"Banana", 'c':"Orange", 'v':"Apple", 's':"Mango"}];
fruits[0].toString();
document.getElementById("demo").innerHTML = fruits;}
can anybody help me please?
You can simply use JSON.stringify:
JSON.stringify(fruits);
jsfiddle

Get element by not having an id/class [duplicate]

This question already has answers here:
How to Select Element That Does Not have Specific Class
(6 answers)
Closed 7 years ago.
I want to select all elements that don't have a specific class, how can I do this ?
I would like to have something like :
var test = document.getElementsByClassName(!"class");
Use
var test = document.querySelectorAll(":not(.class)");
But you'd better be a little more precise, for example targeting only divs:
var test = document.querySelectorAll("div:not(.class)");

How can I change the text using JavaScript [duplicate]

This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 7 years ago.
<div class="basePrice">$99.99</div>
I want to change the value $99.99 using JavaScript.
I tried using getElementByClass but it didn't produce the results that I was hoping to get.
document.getElementsByClass returns a NodeList, which is kind of like an array.
You have to specify which element (there's only one here, so I'm assuming the first) you're looking for, and then you can use .textContent to change the text of the node.
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
<div class="basePrice">$99.99</div>
You can do that like so
document.querySelector('.basePrice').innerHTML = 'different text'
FIDDLE
try getElementByClassName I believe is the proper syntax.

Categories