0

i have 2 input of type text (Id and name). I want to search id in my database and find name related to this id and set it in input of name and also disable this input. For this mean i write a function(with java language) that get name related to id input. But i test it with a submit button that is not what i want.1) I want to call function when id input is fill 2)also the name input disable option set to true, when id input is empty it change to false This is my jsp code:

<input type="text" id="id">
<input type="text" id="name">
<input type="submit" formaction="myFunc">
BeginnerProgrammer
  • 664
  • 3
  • 13
  • 27
  • It's not the same like a java function. The action if not settled is generated from Struts tag, and it defaults to action attribute. The action attribute itself is used to specify the action name. The action name and namespace both define the source for request URL, which is used to transmit the formdata object to the server. – Roman C Dec 14 '17 at 01:24

2 Answers2

0

You should use onChange event, that is described here: https://www.w3schools.com/jsref/event_onchange.asp

Marek
  • 3,935
  • 10
  • 46
  • 70
  • I don't want to call a js function. myFunc is a function write with java language( i'm using struts). Can i call it in this way? – BeginnerProgrammer Dec 10 '17 at 16:39
  • Yes, I think you can. Just write onChange="myFunc". This way your function will be called each time there is some change (key pressed) – Marek Dec 12 '17 at 00:46
0

As @Marek said, use the onchange event to call a JavaScript function. Then see this post for how to disable an input with JavaScript: How to disable an input type=text?

Ryan Walker
  • 715
  • 2
  • 10
  • 23
  • I try this. For first time that i fill the id input this work true and name input is change to disable. But when i clear the text box of id input the name text box do not change to enable mod. I test this code in id input but the result was not true onchange="document.getElementById('name').disable=(''!=this.val)" – BeginnerProgrammer Dec 10 '17 at 16:42
  • Add an `if` statement that checks if the id input is empty. If it's empty, then set disabled to false. – Ryan Walker Dec 10 '17 at 16:45
  • `if (document.getElementById('id').value == '') { document.getElementById('name').disabled = false; }` – Ryan Walker Dec 10 '17 at 16:49
  • Thank you, document.getElementById('name').disable=(''!=this.‌​val) didn't do this?? – BeginnerProgrammer Dec 10 '17 at 16:58
  • Cool if it worked then please mark as correct :) In JavaScript, `this` does not refer to the left side argument. Usually, it refers to the global variable identified or the parent function. http://unschooled.org/2012/03/understanding-javascript-this/ – Ryan Walker Dec 10 '17 at 21:28