1

It's HTML Code:

<tr>
<td class="caption">Amount</td>
<td><input type="text" name="Amount" value="" size="30" id="Amount" 
onkeypress="return isNumberKey(event);" onchange="this.value = 
changeAmount(this.value);" maxlength="18"></td>
</tr>
<tr>
<td>Amount Word</td>
<td><textarea name="AmountWord" cols="" rows="" wrap="soft" 
class="text_sotien" id="AmountWord" readonly=""></textarea></td>
</tr>

My website will have 2 field as above. changeAmount() function is a script on my server. Now, I wanna create a Javascript script for my customer. When they pasted it on your browser console (Ctrl + Shilf + I on Chrome or F12 on Firefog), it will fill new value of Amount field then call function changeAmount() to change value of AmountWord. It's my code but it only can change value of Amount field, not call changeAmount() function to change value of AmountWord field :(

javascript:document.getElementById("Amount").value='111000';document.getElementById("AmountWord").focus; document.getElementById("AmountWord").click();

Please help me

AskToBeAsked
  • 11
  • 1
  • 4
  • Possible duplicate of [How to trigger event in JavaScript?](http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript) – Ahmet Emre Kilinc Mar 31 '17 at 11:07

1 Answers1

2

With modern browser methods:

const input = document.querySelector("input[name='Amount']")
input.dispatchEvent(new CustomEvent("change"))

Better to just avoid inline JS calls like that though.

gunn
  • 8,999
  • 2
  • 24
  • 24
  • var input=document.getElementById("Amount"); a simple note as the input has a ID so no need to use querySelector,replace the first line with this – Pranoy Sarkar Mar 31 '17 at 11:32
  • Thank you for your reply. But it still don't work. I used the code: const input = document.querySelector("input[name='Amount']") input.value = 523553 input.dispatchEvent(new CustomEvent("change")) true It changed value on Amount field to 523553 but still don't call function changeAmount() (So that value of AmountWord not change too). Can you test it again? – AskToBeAsked Mar 31 '17 at 12:49
  • Note that I don't want create new event, I only want call function changeAmount() when I changed value of Amount field via Browser Console – AskToBeAsked Mar 31 '17 at 13:05