0

I'll give you an example of what I'm trying to do.

In Javascript:

document.innerHTML += "<input type='button' id='moveButton' value='a' onclick='webInput('a');'>"

The problem is that the html being written has two levels of quotes, and I can't figure out how to format the second level of quotes here:

webInput('a')

In my Javascript file, I have one function write the inputs into the HTML at the end of the function, then clicking the inputs calls the next function, until eventually the inputs are rewritten.

I know the answer probably has something to do with Escape Characters, but I was confused as to how to format them because the text is being passed back and forther between Javascript and HTML, which use different Escape Characters.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
Spralg
  • 1

2 Answers2

1

You can create your button within the javascript code and define the method therein.

var button = document.createElement('button');
button.addEventListener('click',function(){
   doSomething();
});
//This method adds your button to body
document.body.appendChild(button)
Ziya ERKOC
  • 839
  • 7
  • 18
-1

If you have requirements to add this as string to the document, you need to escape quotes:

"<input type='button' id='moveButton' value='a' onclick='webInput(\"a\")'>"

great answer with rules for escaping here: https://stackoverflow.com/a/16134942/5727598

Also, you should use innerHtml property for element in which you want to pass your string but be sure it will remove all content there;

If no requirements, create button and upend to the body:

var input = document.createElement('input');

input.type = 'button';
input.id = 'moveButton';
input.value = 'a';

button.addEventListener('click', function(){
   webInput(this.value); //if you need to pass input value as parameter
})

document.body.appendChild(input);
rossoneri
  • 443
  • 5
  • 17