-3

ı want to create a button with link but it does not work can you help me

thats my code :var btn= document.createElement("button");btn.innerText="yeni tuş";document.body.appendChild(btn);

ı try create div and add href into it but did not work

var dv= document.createElement("div");dv.innerHTML = " <button>new</button> " dv.style.color = 'red'; dv.setAttribute('href', 'www.google.com'); document.body.appendChild(dv);

1 Answers1

2

Either create an a element and style it to look like a button or create a button element and make clicking it set window.location.

const aElem = document.createElement('a');
aElem.href = 'https://google.com';
aElem.innerHTML = 'link';
aElem.classList.add('btn');
document.body.appendChild(aElem);

document.body.appendChild(document.createElement('br'));
document.body.appendChild(document.createElement('br'));

const btnElem = document.createElement('button');
btnElem.onclick = () => window.location = 'https://google.com';
btnElem.innerHTML = 'button';
document.body.appendChild(btnElem);
.btn {
  text-decoration: none;
  background-color: #EEEEEE;
  color: #333333;
  padding: 0px 6px 2px 6px;
  border: 1px solid #666;
  border-radius: 3px;
}
Rocky Sims
  • 3,523
  • 1
  • 14
  • 19