Reputation: 598
In the next lines I replace word 'first' with word 'second' and then the user may click on word 'second'. I wonder if there is some way to enter the mimic of click inside <span onclick="myFunction()">second</span>
so as the user not to have to click on word 'second'. It has to run instantly. Please keep in mind that I am not interested just in running 'myFunction' but mimicing implicitely the click.
<div id = 'FirstSentence'>This is the first sentence</div>
<div id = 'SecondSentence'></div>
<div id = 'itWorked'></div>
<script>
jSentence = document.getElementById('FirstSentence').innerHTML
jSentence = jSentence.replace('first', `<span onclick="myFunction()">second</span>`)
document.getElementById('SecondSentence').innerHTML = jSentence
function myFunction() {
document.getElementById('itWorked').innerHTML = 'it worked !!!';
}
</script>
Upvotes: 0
Views: 75
Reputation: 1828
You just have to call click function on the element, please see the example below.
jSentence = document.getElementById('FirstSentence').innerHTML
jSentence = jSentence.replace('first', `<span onclick="myFunction()">second</span>`)
document.getElementById('SecondSentence').innerHTML = jSentence
function myFunction() {
document.getElementById('itWorked').innerHTML = 'it worked !!!';
}
document.getElementById('SecondSentence').querySelector('span').click();
<div id = 'FirstSentence'>This is the first sentence</div>
<div id = 'SecondSentence'></div>
<div id = 'itWorked'></div>
Upvotes: 1