Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have problems activating a simple element in HTML via JavaScript.
So I have a simple Setup:
<a href="example.com">
</a>
<button>Klick</button>
Now I want, that if the button is clicked, some JS Code will be executed, which then "Clicks" the element. Similar to executing the .click() function on a button.
I also use jQuery, if that makes it easier for you. Thanks in advance.
native Js has a click() method on every elements, you just need to call it
let button = document.getElementById("button")
let link = document.getElementById("link")
button.onclick = () => link.click()
<a id="link" href="example.com"></a>
<button id="button">Klick</button>
if your objective is only to have a link looking like a button then using css is better than having 2 elements
as a bonus it allow you to keep all of the <a> default behavior you might lose by using a button
a {
text-decoration: none;
color: inherit;
padding: 2px 25px;
border: 1px solid #b6b6b3;
border-radius: 3px;
background-color: #e0e0df;
}
a:hover {
background-color: #fafafa;
}
I'm a <a>
<button>I'm a <button></button>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
Hello I have looked all over the internet and stack overflow and I can't find any code to make a button connected to a text field to add an element. with the text inside of it with a checkbox next to it so please help me :).
(edit: it hit me before answers came;)
a{
background-color: #4CAF50; /*Original code; w3 schools*/
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
and change the document.createelement('p') to document.createelement('a')
In order to create an element, you need to use document.createElement(). We use an event listener to see when the user clicks the button and then grab the value, create the element, set the anchor tag href, set the value as the text for the anchor tag and then append it to the container.
let btn = document.getElementById('addButton');
let container = document.getElementById('container');
btn.addEventListener('click', function() {
// Get the value of the input
let inputData = document.getElementById('input_field').value;
// Create an anchor element and add text to tag
let newElement = document.createElement('a');
newElement.innerHTML = inputData;
// Set anchor attribute
newElement.setAttribute('href', 'https://www.google.com');
// Attached the new element to the container
container.appendChild(newElement);
// Insert break tags after each anchor element inserted
container.insertAdjacentHTML('beforeend', '<br />');
});
<input type="text" id="input_field">
<button id="addButton">Add anchor tag</button>
<div id="container"></div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to create button effect in which will change its color on click and returns to its original colour after the click.
A conditional was added within the button effect to returns to its original code.
In this case I have four variables storing each color and instantiates for each button id in HTML.
// VARIABLES - ids from DOM instantiated as querySelector().
const btnBlue = document.querySelectorAll("#btnBlue");
const btnGreen = document.querySelectorAll("#btnGreen");
const btnRed = document.querySelectorAll("#btnRed");
const btnYellow = document.querySelectorAll("#btnYellow");
Giving an example of a button here is where I am stuck with the logic.
// Exemple with blue button:
$(btnBlue).click(function() {
$(this).css('background-color', '#00FFFF');
// the play() func does other logic such as join other functions
// together and flash all the buttons in different order.
blueBtnAudio.play();
if(btnBlue == '#00FFFF' ){
$(btnBlue).stop();
}
});
There's no need for any JS here. You can do this with CSS alone by using the :active pseudo-selector:
button {
background-color: #CCC;
border: 0;
border-radius: 5px;
padding: 10px 20px;
outline: 0;
}
button:active {
background-color: #C00;
color: #EEE;
}
<button>Click me</button>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have several pictures of the same button, each one representing it in a different sate: normal, pressed, focused, disabled.
How can I make it into an html button that automatically shows the correct picture (and also has an onClick event) ?
Feel free to use html / css / javascript.
The tag also doesn't need to be a button, it could be an image, , or whatever you want, but hopefully written in a generic enough way for others to use your solution too
Thanks!
Just add a class to a link:
<a href="#" class='styledbutton'>Buttontext</a>
... and some CSS:
.styledbutton {background: url(defaultstate.png); display: inline-block;}
.styledbutton:hover {background: url(hoverstate.png);}
.styledbutton:focus {background: url(focusstate.png);}
You can make use of the CSS pseudo-selectors :hover and :focus to change the state of a button at various different interaction points, and simply tie a function into the onclick event in order to run additional JavaScript if required:
function buttonClick() {
console.log('Button clicked');
}
button { /* Default state */
background: white;
}
button:hover { /* On hover */
background: red;
}
button:focus { /* After a click */
background: blue;
}
<button onclick="buttonClick()">Button</button>
Keep in mind that this can also be done with an image by simply passing the image's path into background-image as a url() value.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My goal is to display text on a screen as a user types it, with an underscore cursor showing after the text. The best way I can describe what I'm trying to do is a similar look to using the command line.
What is the best way to do this? I haven't been able to get it to work using a simple text input with css.
Here's something to get you started with jQuery.
$('input').on('input', function(e){
$('div').empty().text($(this).val());
});
Edit: blinking underscore can be done with CSS
div:after{
content:"_";
opacity:0;
animation:blink .500s linear infinite;
}
#keyframes blink{
from{opacity:0;}
to{opacity:1;}
}
The best way? Read the content on every keyup and then inject it into a destination container to show it to the user. Easy.
Check this: http://jsfiddle.net/VDd6C/8/
Show us your code. We'll help.
A basic one, try this (tested in Chrome and FF)
Play it here
HTML
<div>
<span contenteditable="true">Enter your input: </span><span class="blink">_</span>
</div>
JavaScript
window.setInterval(function(){
$('.blink').toggle();
}, 450);
CSS
body {
background-color: black;
color:white;
font-size: 15px;
font-weight:bold;
font-family: courier;
}
span {
border: none;
}
div {
max-width: 200px;
}
div:focus, span:focus{
outline: 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have found a <div> element with class of .plainMail in a webpage and I want to be able to select all its text by pressing Ctrl+A.
I use Firefox 22. I was thinking about to turn the div.plainMail into a textarea. What can I do?
Update:
Can it be done via document.getElementsByClassName? because with document.getElementById I cannot do it. I use use GreaseMonkey.
I'm not sure about what you're exactly looking for, but you probably need HTML Content Editable attribute.
<div contenteditable="true">
This text can be edited by the user.
</div>
ONLINE DEMO.
By using JavaScript, you could add the attribute/value to the element as follows:
var d = document.getElementById("myelement");
d.setAttribute("contenteditable", "true"); // Or: d.contentEditable = "true";
UPDATED DEMO.
Can it be done via document.getElementsByClassName?
Sure (if you don't care about IE8 and below), but note that getElementsByClassName returns a NodeList of matching elements, not a single element.
Thus, we have to loop through the returned list to apply the attribute, as follows:
var i,
list = document.getElementsByClassName("content");
for (i = 0; i < list.length; ++i) {
list[i].setAttribute('contenteditable', 'true');
}
UPDATED DEMO.
while Googling I found this link. seems very nearest as per this question.
following css code can give a textarea effect to the div.
#div-textarea {
-moz-appearance: textfield-multiline;
-webkit-appearance: textarea;
border: 1px solid gray;
font: medium -moz-fixed;
font: -webkit-small-control;
overflow: auto;
padding: 2px;
resize: both;
}
http://blog.thinkingstiff.com/2012/01/22/how-to-make-a-contenteditable-look-like-an-element-or/
also think link contains this fiddle link..
http://jsfiddle.net/ThinkingStiff/FcCgA/