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','');
Related
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>
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"]');
This question already has answers here:
What is the point of using labels in javascript (label: stuff here) outside switches?
(2 answers)
Closed 5 years ago.
I was reading a book and I am not able to find how this search: is working.
search:while (true){
n++;
for(let i=2;i<=Math.sqrt(n);i++){
if(n%i===0){
continue search;
}
}
Is it predefined in javascript, or what is it?
It's a label, and continue tells the loop to go back to the label.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue#Using_continue_with_a_label
This question already has answers here:
Parse XML using JavaScript [duplicate]
(2 answers)
Closed 5 years ago.
I simply need to extract some info between two tags, in this case, <wsse:BinarySecurityToken>
For example:
<wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is a text I need!!!===</wsse:BinarySecurityToken>
I tried
match = text.match(/<wsse:BinarySecurityToken[.*]>([^<]*)<\/wsse:BinarySecurityToken>/g)
does't work!
Or is there anything better than regex? I use angularJs 1
You want to do:
match = text.match(/<tag1.*>([^<]*)<\/tag1>/g)
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