This question already has answers here:
How to pass arguments to addEventListener listener function?
(36 answers)
addEventListener("click",...) firing immediately [duplicate]
(3 answers)
Closed last month.
I have an input field that I want to delete the value of onblur so ,I wrote a function to do so , it's not working for some reason .
HTML
<input class="add-class" type="text">
javaScript
let addClassInput = document.querySelector(".add-class");
let deleteOnblur = function (ele) {
ele.value = ""
};
addClassInput.addEventListener("blur", deleteOnblur(addClassInput));
It works when I do ...
let deleteOnblur = function () {
addClassInput.value = ""
};
addClassInput.addEventListener("blur", deleteOnblur);
Related
This question already has answers here:
Why my show hide button needs double-click on first time
(3 answers)
How to retrieve the display property of a DOM element?
(4 answers)
Closed 1 year ago.
I have a button in my HTML file:
<button class="nav__btn resbtn" onclick="resumeNavToggle()">Resume</button>
which calls a function from a separate javascript file:
function resumeNavToggle() {
let resBtn = document.querySelector(".resbtn");
let resNav = document.querySelector(".resume__subnav");
let resNavSub1 = document.querySelector(".resume__subnav1");
let resNavSub2 = document.querySelector(".resume__subnav2");
let licNav = document.querySelector(".liccer__subnav");
let couNav = document.querySelector(".courses__subnav");
let licBtn = document.querySelector(".licbtn");
let couBtn = document.querySelector(".coubtn");
if (resNav.style.display === "none") {
resBtn.style.backgroundColor = "#01050a";
resNav.style.display = "grid";
// A BUNCH OF STYLE CHANGES CUT TO MAKE THINGS EASIER
licBtn.style.backgroundColor = "#010a13";
couBtn.style.backgroundColor = "#010a13";
} else {
resNav.style.display = "none";
}
}
But the button is not calling the function on the first click, only on the second click onwards.
Any ideas as to what could be causing this?
This question already has answers here:
React setState not updating state
(11 answers)
Closed 3 years ago.
I have the following code that maintains the value when the textbox value is changed. However, whilst debugging the valueHasChangedEvent the variable x line shown below holds the previous value strangely. Is there something I'm doing wrong? The example shown is when I enter 'test123' into the textbox.
Thanks
onChange event
<Input onChange={this.valueHasChangedEvent}
type="text"
name="test"
id="test" />
Method
valueHasChangedEvent = (event) => {
var self = this;
const { name, value } = event.target;
self.setState({test: value}); // value = 'test123'
var x = self.state.test; // x = 'test12'
}
State needs some time to change, and since you are reading the state value before the state mutates, you get the previous value as output. So you need to write it in the callback to the setState function or read it in shouldComponentUpdate()
var x;
self.setState({test: value}, (x) => {
x = self.state.test
});
This question already has answers here:
jQuery AJAX calls in for loop [duplicate]
(2 answers)
Closed 6 years ago.
http://codepen.io/noczesc/pen/ZWppJQ?
function codeToName(data) {
for (var i = 0; i < data.country.borders.length; i++) {
$.getJSON("https://restcountries.eu/rest/v1/alpha?codes=" + data.country.borders[i], function(json) {
data.country.borders[i] = json[0].name;
console.log(json[0].name);
});
}
};
I'm getting an array of country codes which are supposed to be changed to their full representations in English via the codeToName loop and an API, but it only appends a random name to the end of the array. To get a console.log of it, click on the <body>. It's located in JSONextract.country.borders. The country names grabbed via API are correct (logged in console also), but they don't get assigned to the variables inside my object. How can I solve this issue?
for does not work, you should use $.each
function codeToName(data) {
$.each(data.country.borders, function(index,item){
$.getJSON("https://restcountries.eu/rest/v1/alpha?codes=" + item, function(json) {
data.country.borders[index] = json[0].name;
console.log(json[0].name);
});
});
};
This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
Instead of using :
$('input[id^="appName"]').each(function () {
}
});
I would like to use a varibale for Attribute Starts With Selector
var attrName = "appName";
$('input[id^=attrName]').each(function () {
}
});
The above syntax doesn't work. I checked the jQuery documentation and answers here without success.
try
var attrName = "appName";
$('input[id^=' + attrName + ']').each(function () {
// do stuff here
});
http://jsfiddle.net/swm53ran/156/
This question already has answers here:
Javascript infamous Loop issue? [duplicate]
(5 answers)
Closed 8 years ago.
I have same function that I want to make it based on user input ,
for(var u = 1; u <= tmp; u++){
$('#photo_'+u).change(function(){
$('#src_'+u).val($(this).val());
});
Caption: tmp is user input
I try jshint in jsfiddle showing error : "Don't make function withit loop"
how to make that function loop
My question is based in this Link Here
change id and use class:
$('.photo').change(function(){
$(this).find('.src').val($(this).val());
});
change #photo_... and #src_... to class.
$(this) will to point to current .photo element
no need for a loop in this case...
var functionStr="";
for(var u=1;u<=tmp;u++){
if(u==tmp)
{
functionStr=functionStr+"#photo_"+u;
}
else
{
functionStr=functionStr+"#photo_"+u+",";
}
$(functionStr).change(function(){
$('#src_'+u).val($(this).val());
});