Getting control id in javascript

Hi,
I have 4 different html buttons next to text boxes which when clicked pops up a window a common window called commment.htm.The user types in the comments in the pop up box and press save button,the text is saved in the text box present on the main page.Now i have this script  for the pop up window  
function save(text)
{
  window.opener.document.forms['Form1'].txtAuthorityPosition.value = text;
  self.close();
}
</script>

now can any one tell me how do i pass in the value of different text box using if condition.
THanks.
aspnetdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

callrsCommented:
In the parent window, have a global variable, e.g. named global1
When user clicks button in the main window, set the global variable to indicate the name of  textbox you want filled. E.g.

<input type=button onclick="global1='txtAuthorityPosition';">

Then in the child window:

function save(text)
{
  window.opener.document.forms['Form1'].elements[window.opener.global1].value = text;
  self.close();
}

aspnetdevAuthor Commented:
but in the main window im calling javascript function

      <script>
function editText(e)
{
     text = e.value;
     window.open("edit.htm?currenttext="+text,"editwindow","menubar=no,width=430,height=360,toolbar=no");
}
            </script>

<input onclick="editText(this.form.txtbx)" type="button" value="Edit" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 16px">
ZvonkoSystems architectCommented:
The if condition would work like this:

function save(text) {
  var targetField = "txtAuthorityPosition"; // this is default
  if (condition==1) targetField = "txtAuthorityPosition1";
  if (condition==2) targetField = "txtAuthorityPosition2";

  window.opener.document.forms.Form1[targetField].value = text;
  self.close();
}

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

callrsCommented:
Here's one way:

<input onclick="global1='txtAuthorityPosition'; editText(this.form.txtbx)" type="button" value="Edit" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 16px">

Here's another 2 ways (may need some tweaking or fixing):

<input onclick="global1=this.form.txtbx.name; editText(this.form.txtbx)" type="button" value="Edit" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 16px">

or

function editText(e)
{
   global1=e.form.txtbx.name;
     text = e.value;
     window.open("edit.htm?currenttext="+text,"editwindow","menubar=no,width=430,height=360,toolbar=no");
}


-----
Make sure global1 is defined OUTSIDE of any function in the main window, like this:
var global1;    
ZvonkoSystems architectCommented:
If you extend your button to pass the target field function like this:

<input onclick="editText(this.form.txtbx)" type="button" value="Edit" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 16px">

Then the popup script pass the parameter like this:
<script>
function editText(targetField) {
     fldValue = targetField.value;
     fldName = targetField.name;
     window.open("edit.htm?targetfield="+fldName+"&currenttext="+fldValue,"editwindow","menubar=no,width=430,height=360,toolbar=no");
}
          </script>


And the popup function looks then like this:


function save(text) {
  var targetField = windwo.location.search.split("targetfield=")[1].split("&")[0];
  window.opener.document.forms.Form1[targetField].value = text;
  self.close();
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aspnetdevAuthor Commented:
can you tell me how do i put in condition.Because i got id for html buttons so how do i put it in the condition.

function save(text) {
  var targetField = "txtAuthorityPosition"; // this is default
  if (condition==btn1) targetField = "txtAuthorityPosition1";
  if (condition==btn2) targetField = "txtAuthorityPosition2";

  window.opener.document.forms.Form1[targetField].value = text;
  self.close();
}

if i directly put the values of button in the condition will this work or do i have to use the getelementbyid.

ZvonkoSystems architectCommented:
Forget that if conditions. Take either my querystring parameters in my last post or callrs global var proposal.
ZvonkoSystems architectCommented:
I did not understood your last sentance. Now I do. You need NOT to access the fields always with getElementById() method.
You can do it also traditionaly as I do it alsways do: as form elemets collection:
document.formName.fieldName.value = '123';

And because the field selector can be a String you can do it like this:
document.formName["fieldName"].value = '123';

Or as var String value:
document.formName[targetFieldName].value = '123';

aspnetdevAuthor Commented:
Thank for the quick reply but i guess im not able to convey my question right to you guys.
Actually i have 4 text boxex and 4 buttons next to each text box.On click of the any button it opens a pop up window.But when i fill the pop up window with the text ...its the text box next to the button clicked that should be populated.Then when i click other button then again it pops up comments window i fill in the comments then press the save button then the textbox next to that button is saved.so how do i pass in the value of those.
aspnetdevAuthor Commented:
man..im getting confused now....not able to get this... :(
callrsCommented:
More on global:  When a button is clicked, set a global variable to equal the name of the corresponding input box -- so if button # 1 is clicked, then the global points to input #1, if button # 2 gets clicked  then the global holds the name of input #2 etc.

So all the popup has to do is access that global to fill in the appropriate text field.
callrsCommented:
Solved? lol, just as I strived to explain...
callrsCommented:
Did Zvonko's work?

:-p I broke an appointment to stay here to explain seeing how confused you said you were. Least you could have done was wait for more input. But no part credit even...
ZvonkoSystems architectCommented:
You should not break your appointments outside of EE. That are first signs that you got ill by pointz virus ;-)
callrsCommented:
'tis not mainly about points, though that's a bonus
ZvonkoSystems architectCommented:
Aaaa! Carma! :)
callrsCommented:
Not exactly
callrsCommented:
Karma? Was far from mind. Just a sucker at times. Too helpful for my own good lol. Working for free for years...well now it's got me points...Hmm. but at a site that won't share the answers as freely as it depends on on others...

0 to 1500k+ in 4 1/3 months (134 days on Sept 29); 1.1 Million in the past 2 months once I used Quick Post. Headed to the top 100 in 141 days by Oct 6.

All to prove a point I guess: that I'm not so undeserving of even basic respect and resources so many make me & other progressives out to be.
ZvonkoSystems architectCommented:
The basics of Boolean algebra does not yield always true for negated negative true ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.