How to add a JavaScript event to textarea without having direct access?

advertisements

I'm wondering how to add

onblur="hcb.watermark.blur(event)" onfocus="hcb.watermark.focus(event)" style="color: rgb(136, 136, 136);

to the following textarea without having direct access to it:

<textarea rows="4" id="HCB_textarea" name="content" class="commentbox hcb-shadow-r" onkeypress="hcb.delta(event)"/>

Could some additional JS do the job?


You can grab the element using document.getElementById and manipulate it from there.

var textArea = document.getElementById('HCB_textarea');
textArea.onblur = hcb.watermark.blur;
textArea.onfocus = hcb.watermark.focus;
textArea.style.color = 'rgb(136, 136, 136)';