[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

populate text box

What I need to do is allow user to populate a text box based on link or graphical selection prior to submitting without a page reload.

For example if the user clicked A, then B then C....the value of name would be ABC

<form action="test" method="post">
<input type="text" name="name" value="">
<input type="submit" value="Submit">
</form>
<a href="">A</a>&nbsp;|&nbsp;
<a href="">B</a>&nbsp;|&nbsp;
<a href="">C</a>&nbsp;|&nbsp;
0
riffraff5
Asked:
riffraff5
  • 2
  • 2
1 Solution
 
surajgupthaCommented:
1) Declare a Global Variable
2) On every Onlick event of Href Append the Href's id/ Name / code to the global
3) Assign that global variable to a hidden variable before submitting the form

Regards,
Hope it Helps
0
 
VirusMinusCommented:
<html>
<head>
<title>Untitled</title>
<script language="javascript">
<!--
function addText(word) {
  document.myForm.name.value+=word;
}
//-->
</script>
         
</head>

<body>

<form action="test" method="post" name="myForm">
<input type="text" name="name" value="">
<input type="submit" value="Submit">
</form>
<a href="#" onClick="addText('A')">A</a>&nbsp;|&nbsp;
<a href="#" onClick="addText('B')">B</a>&nbsp;|&nbsp;
<a href="#" onClick="addText('C')">C</a>&nbsp;|&nbsp;

  <table width="100%">
<tr>
    <td><a href="javascript:insertTest('text')">text</a><br>
         <a href="javascript:insertTest('more text')">more text</a></td>
</tr>
</table>
</form>
</body>
</html>
0
 
minichickenCommented:
<script language="javascript">
function add_char(char)
{
      form1.textname.value = form1.textname.value + char;
}
</script>

<form name="form1" action="test" method="post">
<input type="text" name="textname" value="">
<input type="submit" value="Submit">
</form>
<a href="#" onClick="add_char('A');">A</a>&nbsp;|&nbsp;
<a href="#" onClick="add_char('B');">B</a>&nbsp;|&nbsp;
<a href="#" onClick="add_char('C');">C</a>&nbsp;|&nbsp;
0
 
surajgupthaCommented:
If a user clicks a href he actually leaves the page. So there is no simple way it can be done only using javascript.

So i would like to know if the href click would take the user out of the page??
0
 
VirusMinusCommented:
Glad to be of help, riffraff5.

A hyperlink need not always take you do a different page. A hyperlink can be used to go to the top of the page, a quick jump location etc. the '#' makes it go nowhere.

Cheers
-VM
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now