Your question doesn't make much sense. You say:
the source input field will be readonly and thus will only be able to listen to keyboard events.
... but that doens't make much sense. If it's readonly, you cannot focus the field... meaning you cannot give it keyboard events. Regardless, you can always get and set the value of a field, even if it's readonly
or disabled
. If you don't want to use jQuery's val()
- then use plain JS.
$(".src").keyup(function(e){
document.getElementsByClassName('target')[0].value = e.target.value;
});
Update: Now I think I understand your question. Your main goal is that you want to modify the color of the caret by hiding the focused field and writing to another field. Not sure why you want to do this as I assure you your users don't care and you run the risk of interfering with the users experience... and as I type this message I don't even notice the color of the caret in Chrome... buuuut, I have a feeling that you still think that your inputs are more special than others. I feel for your users who try and put the cursor at an earlier point in the input to fix a typo... I digress.
Instead of 2 inputs, create a single input, set it's opacity to 0, and write the entire value to a DIV whenever it changes. This has many benefits:
- You get many of the default behaviors of the input without actually needing to see it (eg. cursor on hover on desktop, keyboard navigation, etc).
- Your form can still do a vanilla submit without needing to copy values or any other javascript weirdness.
- Any other code, validation scripts, etc don't need to change - the input is still getting a value and emitting all events.
https://jsfiddle.net/2sp92bpy/1/