ok here it goes:


I ahve 2 textbox within a form on my HTML page


I want to display the text "SHOUVIK" (without quotes) on the second textbox as soon as I click on the first textbox


Is this possible?

Kindy help

Dani AI

Generated

’s quick answer works and, as reported, it fixes the immediate need. For a cleaner, more maintainable approach avoid inline handlers and attach a focus listener from JavaScript so the behavior is consistent for mouse, keyboard (tab), and scripted focus.

var first = document.getElementById('firstBox');
var second = document.getElementById('secondBox');

function fillSecond() {
  second.value = 'SHOUVIK';
}

if (first.addEventListener) {
  first.addEventListener('focus', fillSecond, false);
} else if (first.attachEvent) {
  first.attachEvent('onfocus', fillSecond);
}

Tips and gotchas: use the focus event rather than click so keyboard users are handled too; if the text is only a hint (not actual input) use the placeholder attribute instead; avoid fragile form indexing like forms[0]—use ids; if it should run once, remove the listener inside fillSecond or use the modern { once: true } option with addEventListener. Place this script after the inputs or run it on DOMContentLoaded so the elements exist. For details on event registration see addEventListener.

Recommended Answers

All 2 Replies

I want to display the text "SHOUVIK" (without quotes) on the second textbox as soon as I click on the first textbox

Make sure that both textboxes have "name" (for older browsers) and "id" attributes.

Add the following to the first textbox:

onfocus="document.forms[0].second_box_name.value='SHOUVIK';"

Thanx mate, that helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.