Link to home
Create AccountLog in
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.
Avatar of callrs
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();
}

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

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
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.

Forget that if conditions. Take either my querystring parameters in my last post or callrs global var proposal.
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';

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.
man..im getting confused now....not able to get this... :(
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.
Solved? lol, just as I strived to explain...
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...
You should not break your appointments outside of EE. That are first signs that you got ill by pointz virus ;-)
'tis not mainly about points, though that's a bonus
Aaaa! Carma! :)
Not exactly
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.
The basics of Boolean algebra does not yield always true for negated negative true ;-)