Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically geting text box value

Status
Not open for further replies.

iffoiffy

Programmer
Feb 24, 2005
67
CA
Hi,

On my form I have suppose 10 text boxes with names
retailprice1
retailprice2
.
.
.
retailprice10

In my javascript function I want to dynamically get the values of those text boxes. I am trying to do something like following but I know it is not right. i will appreciate your help


for (i=1; i <=10; i++)

{

b="document.priceupdater.retailprice" +i+."value";
alert(b);
}


Thanks
 

Use this:

Code:
b = document.forms['priceupdater'].elements['retailprice' + i].value;

Hope this helps,
Dan



[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
try something like:

Code:
for (i=1;i<=10;i++)    
    {       
    b=eval("document.getElementById('retailprice"+i+").value");
    alert(b);
    }

hope that helps!

- g
 
spewn,

I really cannot stress how much I think you should not be using eval for this job - in fact for almost any job. There are only very few times when using eval should ever be recommended.

This is just one of the (more printable) links available when searching on Google for "reasons not to use eval javascript"


That site said:
In the majority of cases, eval is used like a sledgehammer swatting a fly -- it gets the job done, but with too much power. It's slow, it's unwieldy, and tends to magnify the damage when you make a mistake. Please spread the word far and wide: if you are considering using eval then there is probably a better way. Think hard before you use eval.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]
 

That, and that getElementById is designed to get elements by their IDs, not by their NAMEs ;o)

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
thanks a lot for your replies. if my if statement turnout to be true I want to come out from the javascript function if not then I submit the form. I am doing the following it seems to be working , but is this the right way?
************************
for (i=1; i <= numberofprod; i++)

{

if ( document.forms['priceupdater'].elements
['retailprice' + i].value == "")

{ alert ("Can not have blank field")
document.forms['priceupdater'].elements
['retailprice' + i].focus();
return false;
}
}

document.priceupdater.submit()
*************************
 

If you are calling it via the onbsubmit event handler of the form, you do not need to csall the submit method. Just returning true will be enough.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
do you see anything wrong with the following statement

document.forms['priceupdater'].elements['retailprice' + i].value = Removespaces(document.forms['priceupdater'].elements['retailprice' + i]);

when I add this statment on top wierd things start to happen like, alert will get into infinite loop , or if i don't have alert if statment does not get executed


for (i=1; i <= 2; i++)
{
var test ="yes";

document.forms['priceupdater'].elements['retailprice' + i].value = Removespaces(document.forms['priceupdater'].elements['retailprice' + i]);

if (test == "yes")
{
alert("hello");
}


if ( document.forms['priceupdater'].elements
['retailprice' + i].value == "")

{ alert ("Can not have blank field")
document.forms['priceupdater'].elements
['retailprice' + i].focus();
return false;
}


}
 
>do you see anything wrong with the following statement
>document.forms['priceupdater'].elements['retailprice' + i].value = Removespaces(document.forms['priceupdater'].elements['retailprice' + i]);

It depends on your Removespaces(). It can be okay if it takes in an element and return a trimmed element's value. But a guess for more usual routine would be string-in and trimmed string-out. In that case it would be:

[tt]document.forms['priceupdater'].elements['retailprice' + i].value = Removespaces(document.forms['priceupdater'].elements['retailprice' + i][red].value[/red]); [/tt]
 
Removespaces seems to be working fine, it does take the spaces out.

**************
function Removespaces(instring)
{
x=instring.value


prev =0

returnstring = ""
for (i=0; i< x.length; i++)
{
if (x.charAt(i) != " " )
{
returnstring += x.substring(++prev,i)
}
else
{
++prev
returnstring += ""
}
}

return returnstring
}


***********

normally thats what i do do and it works fine
document.addphone.retail_price.value = Removespaces(document.addphone.retail_price)

I will try your code, by adding .value at the end...
 
iffoiffy,

No, no need to try. Because you instring is actually an element, not a simple string. (You have x=instring.value up there.) My post is based on zero-knowledge of your function. As to the reason why your script in question does not work, I or other have to look elsewhere.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top