How using function with another Elements [duplicate] - javascript

This question already has answers here:
Get element from which onclick function is called
(9 answers)
Closed 1 year ago.
I want to color these elements using the same function. When I click on one of them, the same element that was clicked will be colored. How is that done using JavaScript
<button class="btn" onclick="color()">A</button>
<button class="btn"onclick="color()">B</button>

const $button = document.querySelector('.btn')
$button.addEventListener('click', () => {
$button.style.color = "#0099ff"
})

Related

change title with a simple button [duplicate]

This question already has answers here:
What does innerHTML do in javascript?
(11 answers)
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed last month.
I can't fix this simple problem, I created a simple button and then a title which says "hello" .
If you press that button the text of the title should be changed for another text (any text I donĀ“t care).
If I use jQuery it works perfectly, but I want to do this without jQuery just for learning.
document.querySelector(".btn").addEventListener("click", function() {
document.querySelector("h1").innerHTML("hola como estas")
});
<h1 class="title">HELLO</h1>
<button class="btn" type="button"> Click me</button>
I use all the getElement and more and it doesn't work, it's supposed to change the text.
const btn = document.querySelector("button");
const chngTxt = document.querySelector("h1");
btn.addEventListener("click", function () {
chngTxt.innerHTML = "hola como estas";
})
<h1>Hello</h1>
<button id="changeText">click me</button>
or you can just change your innerHTML to quotation mark
document.querySelector(".btn").addEventListener("click", function(){
document.querySelector("h1").innerHTML = "hola como estas";
});

Hwo do I display the output of a javascript function through HTML [duplicate]

This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 1 year ago.
Im trying to create a calculator for a formula. This function works, I have seen in the console. I just can't figure out how to display the output.
enter image description here
Here it goes;
Like the comment said there are different way and all depends on your context and UI.
Here is quick possibility how you can take your own code forward to show html;
DOM Inner HTML property
I have just used a button to trigger the function with runs your calculation function and then updates the div that we predefined. This part could be any thing for that matter.
function add(a,b){
return a+b;
}
console.log(add(1,4));
var displayhtml = document.querySelector('div');
var trigger = document.querySelector('button');
trigger.onclick = function (){
displayhtml.innerHTML = add(1,4);
}
<div>I am div</div>
<button>chnage html</button>

How to access strict/explicit href value with Javascript? [duplicate]

This question already has answers here:
Get local href value from anchor (a) tag
(6 answers)
Closed 2 years ago.
Given the following anchor:
<a class="example" href="record1234">link</a>
How can the actual value of href be retrieved with vanilla Javascript?
const link = document.querySelector('.example');
console.log(link.href);
// https://exampledomain.com/current/document/path/record1234
console.log(link.pathname)
// /current/document/path/record1234
// with jQuery
$('.example').attr('href');
// what I want to do:
console.log(link.href.string); // record1234
Use .getAttribute on the element.
const link = document.querySelector('.example');
console.log(link.getAttribute('href'));
<a class="example" href="record1234">link</a>

Check and uncheck checkbox with vanilla javascript [duplicate]

This question already has answers here:
toggle checkbox with javascript
(4 answers)
Closed 2 years ago.
Very basic question here, I am using a JS library to add keyboard shortcuts, I want to check and uncheck a checkbox using the same line of commands, but I am not sure about how to do this, here is the code:
shortcuts.add('alt+s', function() {
document.getElementById("menu-toggle").checked = true
})
How can I use the same shortcut to uncheck the same element?
document.getElementById("menu-toggle").checked = false;
Thanks in advance!
I recommend Element.toggleAttribute:
const toggleChecked = () => document.getElementById('menu-toggle').toggleAttribute('checked');
document.getElementById('myBtn').addEventListener('click', toggleChecked);
<input id="menu-toggle" type="checkbox"> Check me
<br><br>
<button id="myBtn">Click me</button>

jquery get css property of clicked element [duplicate]

This question already has answers here:
Get "original" (non-hover) background color of object when hovering over it
(6 answers)
Closed 4 years ago.
I'm trying to get the background color of the clicked element, but I get the value for: hover. This is my code:
$(button).click(function() {
var bgColor = $(this).css('background-color');
console.log(bgColor);
});
What am I doing wrong?
You can use event.target
$(button).click(function(e) {
var bgColor = $(e.target).css('background-color');
console.log(bgColor);
});

Categories

Resources