Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,744 members, 7,817,056 topics. Date: Saturday, 04 May 2024 at 01:35 AM

How Can I Make This Javascript Work - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How Can I Make This Javascript Work (1201 Views)

How Do I Make Profit From My Facebook Page Of Over 587k Likes? / I Show You How I Make $1,000 Per Day With Facebook Viral Script On My Blog / How Did Google Make This Cool Google Doodle And Can U Make Urs? (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Make This Javascript Work by binaflexia: 10:54pm On Nov 11, 2011
I have 3 HTML form inputs fields that is dynamically generated by a "add more" button, with naming for the fields name as fieldName, fieldName1, fieldName2, fieldName3,  and so on.

Now, I'm trying to retrieve the value from this fields with JavaScript, using the script below.
var bookingForm = document.forms['formName'];
var qty = bookingForm.fieldName +'i'.value;


with the 'i' been a generated numeric number by a for loop
when I use alert(qty), it returns NaN, when I'm expecting the value for fieldName1, fieldName2,  and so on.

But when I use;
var qty = bookingForm.fieldName.value
I can get the value in that field but get NaN when I try to concatenate 1,2,3,  with the fieldName.

Any help will be very much appreciated.
Re: How Can I Make This Javascript Work by Therages: 11:24pm On Nov 11, 2011
please drop the full javascript code, so i or sum1 else cn figure out d error. And try debugging using firebug. Its a firefox extension
Re: How Can I Make This Javascript Work by Therages: 12:04am On Nov 12, 2011
//try dis
//
avoid global variables
//enclose ur code in a function
var i = 3,
bookingForm =
document.forms['formName'];
while(i--){

var qty =
bookingForm.fieldName
+'i'.value;
}
//but if d order matter
//u can use a for loop insted i.e
var bookingForm =
document.forms['formName'];
for(var i =0; i<=3;i++){
var qty =
bookingForm.fieldName
+'i'.value;
}
Re: How Can I Make This Javascript Work by binaflexia: 12:08am On Nov 12, 2011
Therages:

//try dis
//
avoid global variables
//enclose your code in a function
var i = 3,
bookingForm =
document.forms['formName'];
while(i--){

var qty =
bookingForm.fieldName
+'i'.value;
}
//but if d order matter
//u can use a for loop insted i.e
var bookingForm =
document.forms['formName'];
for(var i =0; i<=3;i++){
var qty =
bookingForm.fieldName
+'i'.value;
}


Thanks for your time. I will try that and get back to you.
Re: How Can I Make This Javascript Work by BoOOosHBoy: 4:12am On Nov 12, 2011
Well?
Re: How Can I Make This Javascript Work by binaflexia: 11:50am On Nov 13, 2011
Still not working. Any help?
Re: How Can I Make This Javascript Work by yawatide(f): 12:54pm On Nov 13, 2011
Taking a bird's eye view (I would have thought you would post the html code for the form):

1) Were there any values in the form fields when you tried to access their values?

2) IMHO, I think it makes more sense for you to: Use the DOM, via an ID, to get a count of fields that aren't empty, store those in an array, then loop over that array to get their values

Direct access to any vars when it comes to programming in general, doesn't make for readable/understandable code
Re: How Can I Make This Javascript Work by Nobody: 1:18pm On Nov 13, 2011
You cannot just be posting riddles na. (The Riddler vs Batman) - what goes bang in the middler a river when the church bell strikes 12 midnite. . .
Re: How Can I Make This Javascript Work by Therages: 11:18am On Nov 14, 2011
@binaflex
i'v also tried it, didn't work, n dnt knw y. Buh u cn fix it by givin d elements name n id 2geda.
Iz nt abt d moni @dhtml.

<html>
<head>
<script type="text/javascript">
function displaymessage()
{var i=3;
while(i--){
alert(document.getElementById('pp'+i).value);
}
}
</script>
</head>

<body>
<form name='fnm' id='bb'>
<input name='inm' 'id='pp2' type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>
Re: How Can I Make This Javascript Work by binaflexia: 12:08am On Nov 15, 2011
Therages:

@binaflex
i'v also tried it, didn't work, n dnt knw y. Buh u cn fix it by givin d elements name n id 2geda.
Iz nt abt d moni @dhtml.

<html>
<head>
<script type="text/javascript">
function displaymessage()
{var i=3;
while(i--){
alert(document.getElementById('pp'+i).value);
}
}
</script>
</head>

<body>
<form name='fnm' id='bb'>
<input name='inm' 'id='pp2' type="button" value="Click me!" onclick="displaymessage()" />
</form>
</body>
</html>


yawa-ti-de:

Taking a bird's eye view (I would have thought you would post the html code for the form):

1) Were there any values in the form fields when you tried to access their values?

2) IMHO, I think it makes more sense for you to: Use the DOM, via an ID, to get a count of fields that aren't empty, store those in an array, then loop over that array to get their values

Direct access to any vars when it comes to programming in general, doesn't make for readable/understandable code

Thank you very much for your effort. Though I've already done it but I really appreciate you taking your time to reply my post
Re: How Can I Make This Javascript Work by micodon(m): 11:28pm On Nov 18, 2011
Next Time post the full code. i think innerHtml shld solve ur problem

(1) (Reply)

How To Register A Domain Name For Free / Monetising Opera Mini Traffic / Make Money Easily With Cashpiggy Online Get Paid For Clicking Ads

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 27
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.