This question already has answers here:
How to insert an element after another element in JavaScript without using a library?
(20 answers)
Closed 4 years ago.
How to add line breaks between buttons in javascript? I want break tag
to be outside of the button elements.
html:
<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>
js:
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.appendChild(lineBreak);
gives:
<button id="buttonOne"> click one me <br> </button>
<button id="buttonTwo"> click on me too </button>
what I want is:
<button id="buttonOne"> click one me </button>
<br>
<button id="buttonTwo"> click on me too </button>
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.parentNode.insertBefore(lineBreak, buttonOne.nextSibling);
<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>
Try:
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.parentNode.insertBefore(lineBreak, buttonOne.nextSibling);
after() should do it:
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.after(lineBreak);
<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>
Related
<button class="btn" onclick="func(0)" value="">abc</button>
<button class="btn" onclick="func(1)" value="">def</button>
<button class="btn" onclick="func(2)" value="">ghi</button>
<script type="text/javascript">
function func(i){
var btn= document.getElementsByClassName("btn")[i];
console.log(btn);
btn.style.color="red";
}
</script>
I want to add new button every time, and want to display them on top. For adding them on top i need to change numbering till end
any solution to this. How new button[i] can be displayed on top
Subtract from the number of buttons to count from the end.
function func(i) {
var all_buttons = document.getElementsByClassName("btn");
all_buttons[all_buttons.length - i - 1].style.color = "red";
}
<button class="btn" onclick="func(2)" value="">abc</button>
<button class="btn" onclick="func(1)" value="">def</button>
<button class="btn" onclick="func(0)" value="">ghi</button>
How can I write a function that hides a specific button using vanilla js?
<button class"btn">button 1 </button>
<button class"btn">button 2 </button>
<button class"btn">button 3 </button>
For example what I want is when I click button 2, button 1 and 3 will be hidden.
You can use document.querySelector("button:nth-child(2)") for getting 2nd button, addEventListener and style.display for your requirement.
var second = document.querySelector("button:nth-child(2)");
second.addEventListener("click", button2click);
function button2click() {
var first = document.querySelector("button:nth-child(1)");
var third = document.querySelector("button:nth-child(3)");
first.style.display = 'none';
third.style.display = 'none';
}
<button class"btn">button 1 </button>
<button class"btn">button 2 </button>
<button class"btn">button 3 </button>
first of all, thank you for your time to read this question, and two things, I'm using ES5 and I don't use jQuery.
Right now I'm struggling a lot to figure what's the correct solution for the addEventListener, because for some reason it does not trigger for the second button which is only for the mobile screen dimensions, the problem is that the second button have the same id but different class, for example this:
<div class="product-bg-container product-general-info variation-info">
<input type="hidden" name="sku" value="Something-15892290" id="selected-option">
{/* Desktop screen button */}
<button id="buy-now" class="btn btn-lg hidden-sm-down btn-primary">
Add to Cart
</button>
{/* Mobile screen button */}
<button id="buy-now" class="btn btn-lg hidden-md-up btn-primary">
Add to Cart
</button>
</div>
Where I am trying to trigger the second button but it does not where I don't understand why it does, if the id is the same, should not matter, so I'm trying to figure how to trigger from the first button if it's clicked and also with the second if it's clicked, but I'm out of ideas...
var button = document.getElementById('buy-now');
if (!button) {
return;
}
button.addEventListener('click', function trackAddToCart() {
// more code for the event
}
I thought an idea to capture the attribute of the button, but it works in the first button but not for the second one:
var button = document.getElementById('buy-now');
var att = button.getAttribute('class');
button.addEventListener('click', function() {
console.log('class ' + att); //shows: class: btn btn-lg hidden-sm-down btn-primary
console.log('button class? '+ button); //shows: button element: [object HTMLButtonElement]
});
But when I click the second button... does not trigger or happening nothing, not sure why... and I can't change the id value (which it should be easy but I can't "company standard")
Can anyone help me to have an idea how to capture and trigger the event for the second button ??
The attribute id must be unique in a document. You can use attributeStartsWith selector or class with querySelectorAll(). Then loop through all the button to attach the event (click) individually:
//var button = document.querySelectorAll('.btn.btn-primary');
var button = document.querySelectorAll('[id^=buy-now]');
button.forEach(function(btn){
btn.addEventListener('click', function() {
console.log('class ' + this.classList);
console.log('button class? '+ this.id);
});
});
<div class="product-bg-container product-general-info variation-info">
<input type="hidden" name="sku" value="Something-15892290" id="selected-option">
<button id="buy-now" class="btn btn-lg hidden-sm-down btn-primary">
Add to Cart
</button>
<button id="buy-now2" class="btn btn-lg hidden-md-up btn-primary">
Add to Cart
</button>
</div>
nextElementSibling seems working in this case.
var btn1 = document.getElementById("btn");
var btn2 = btn1.nextElementSibling;
btn1.addEventListener("click",function(e){
console.log("btn1");
});
btn2.addEventListener("click",function(e){
console.log("btn2");
});
<div>
<button id="btn" class="btn1">butotn 1</button>
<button id="btn" class="btn2">butotn 2</button>
</div>
I want to create these buttons:
<!DOCTYPE html>
<html>
<body>
<button type="button">Linear algebra </button>
<button type="button">Calculus I </button>
<button type="button">Basic Mechanics </button>
<button type="button">Mechanics1 </button>
<button type="button">Mechanics2 </button>
<button type="button">Mechanics3 </button>
<button type="button">Mechanics4 </button>
<button type="button">Mechanics5 </button>
</body>
</html>
And I was wondering if there is an option to do it with a function for when the list is even longer.
My idea is to create the following list:
var subjects=["Linear algebra","Calculus I","Basic Mechanics","Mechanics1","Mechanics2","Mechanics3","Mechanics4","Mechanics4"]
And iterate it with a for loop and then create the button with the result of every element in the list. Is this possible?
You can use Array forEach to iterate through the array, use createElement to create the buttons and appendChild to append them to the DOM where needed.
var subjects=["Linear algebra","Calculus I","Basic Mechanics","Mechanics1","Mechanics2","Mechanics3","Mechanics4","Mechanics4"]
subjects.forEach(function(item){
var button = document.createElement('button');
button.type = 'button';
button.innerHTML = item;
document.body.appendChild(button);
})
I have ten buttons with a button for next and previous,
when I click the next button, it should show the next two buttons (hiding the rest).
The reverse should happen when the previous button is clicked (Show the previous two buttons (hide the rest)).
thanks.
my html code is :
<div>
<button class="menu">M1</button>
<button class="menu">M2</button>
<button class="menu">M3</button>
<button class="menu">M4</button>
<button class="menu">M5</button>
<button class="menu">M6</button>
<button class="menu">M7</button>
<button class="menu">M8</button>
<button class="menu">M9</button>
<button class="menu">M10</button>
</div>
<div>
<button class="action" id="btnNext">Next</button>
<button class="action" id="btnPreview">Previous</button>
</div>
Initially make every button hidden except first two.
$("#btnNext").on('click', function(){
var vBtn = $(".menu:visible:last");
$(".menu").hide();
vBtn.next().show();
vBtn.next().next().show();
});
$("#btnPreview").on('click', function(){
var vBtn = $(".menu:visible:first");
$(".menu").hide();
vBtn.prev().show();
vBtn.prev().prev().show();
});
I manually coded not tested please check
$("button.menu").not(':eq(0),:eq(1)').hide();
var count = 1;
$("#btnNext").click(function() {
$("button.menu").hide();
count = count + 2;
$("button.menu").eq(count-1).show();
$("button.menu").eq(count).show();
});
$("#btnPreview").click(function() {
$("button.menu").hide();
count = count - 2;
$("button.menu").eq(count-1).show();
$("button.menu").eq(count).show();
});