print/alert array in javascript [duplicate] - javascript

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

Related

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

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>

.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"]');

how can i get the parameter using window.location.search.substring [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 4 years ago.
I have this URL https://localhost/test.html?view=1111&project=1000.
How would i write java script function to fetch the variable project..?
A cool trick is to split your URL with project= and take the second element :
const url = "https://localhost/test.html?view=1111&project=1000"
console.log( url.split("project=")[1] )

javascript type conversion arrays to string,toString not achieve [duplicate]

This question already has answers here:
javascript - Convert array to string while preserving brackets
(2 answers)
Closed 5 years ago.
[1,2].toString() is "1,2",toString() cannot achieve
how to turn arrays[1,2] into string "[1,2]"
var a=[1,2,3]
var b=a.toString()
b="1,2,3"
Try this it may help you.
var a=[1,2,3];
var b=a.toString();
b = '"'+b+'"';
alert(b);

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','');

Categories