1

I am trying to add <button id="mybtn"></button> around the some text link but I am not sure how to make it. I tried a bit with document.write but I think I am using it wrong. When I click this button some text it replaces 'ZZZ' with nothing. However the changed link something here has a plain formatting. What I want it to do is add the <button id="mybtn"> and</button> around it, so the changed button stays with the same look.

Input:

document.getElementById("DiacriticsButton").innerHTML = "some text";

Output should be something like:

document.getElementById("DiacriticsButton").innerHTML = <button id="mybtn">"some text"</button>;

The current full script:

<script>
bodyText=document.getElementById("bodytext").innerHTML;
clearText = bodyText.replace(/ZZZ/g, "");
FStr = clearText.search("some text");

do {
  clearText = clearText.replace("something here","some text");  
  FStr = clearText.search("something here");
}
while(FStr!=-1);

function switchDiacritics() {
  if (document.getElementById("bodytext").innerHTML == clearText) {
    document.getElementById("bodytext").innerHTML = bodyText;
    document.getElementById("DiacriticsButton").innerHTML = "something here";
  } else {
    document.getElementById("bodytext").innerHTML = clearText;
    document.getElementById("DiacriticsButton").innerHTML = "some text";
  }
}

</script>
1

2 Answers 2

1

Do you look for something like this

document.getElementById("DiacriticsButton").innerHTML = '<button id="mybtn">"some text"</button>';

It is also possible to add element like this

var btn = document.createElement("button");
btn.innerHTML = "some text";
document.getElementById("DiacriticsButton").appendChild(btn);

Sign up to request clarification or add additional context in comments.

Comments

0

Are you doing anything to trigger this innerHTML change? Ex:

Wrapping the selection in window.onLoad{...}

OR

Wrapping it in a function to be triggered by an event listener. While if your goal is to simply place text within the tag in a static demeanour use the HTML attribute for a button with value="some text"

7 Comments

Thanks for replying, I've updated the questions and added the whole script.
So when are you trying to trigger your switchDiacritics function? Is it on the pages load or an event listener ex. (click on certain elements).
triggering it with <a href="javascript:switchDiacritics()" ID="DiacriticsButton"><button id="mybtn">some text</button></a>
You can trigger it by simply placing a onclick attribute to your anchor tag: <a onclick="switchDiacritics()" ID="DiacriticsButton">, just assure you define the function before it's used.
Can you edit the code as suggested and post it as an answer, so I can test it and accept the answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.