Avatar of aspnetdev
aspnetdev
 asked on

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.
JavaScript

Avatar of undefined
Last Comment
Zvonko

8/22/2022 - Mon
callrs

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();
}

aspnetdev

ASKER
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">
Zvonko

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();
}

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
callrs

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;    
ASKER CERTIFIED SOLUTION
Zvonko

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
aspnetdev

ASKER
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.

Zvonko

Forget that if conditions. Take either my querystring parameters in my last post or callrs global var proposal.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Zvonko

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';

aspnetdev

ASKER
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.
aspnetdev

ASKER
man..im getting confused now....not able to get this... :(
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
callrs

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.
callrs

Solved? lol, just as I strived to explain...
callrs

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...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Zvonko

You should not break your appointments outside of EE. That are first signs that you got ill by pointz virus ;-)
callrs

'tis not mainly about points, though that's a bonus
Zvonko

Aaaa! Carma! :)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
callrs

Not exactly
callrs

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.
Zvonko

The basics of Boolean algebra does not yield always true for negated negative true ;-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.