Accessing Product_Attributes[6]:value with JS

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Accessing Product_Attributes[6]:value with JS

Post by Luke »

JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I need to make a really quick and dirty javascript where the user can choose from one of 3 or 4 options, and when they click on it, it will appear in the following text box:

Code: Select all

<textarea name="Product_Attributes[6]:value" rows=10 cols=58 wrap="on"></textarea>
Here is what my code looks like (MY js skills SUCK). I don't really have time to do a good job on this, and I'm not getting paid to do a good job, but it needs to get done ASAP. This isn't working and I Can't figure out why:

Code: Select all

<script language="javascript" type="text/javascript">
var saying = new Array();
saying[0] = "The {your last name} family wishes to announce the new addition to our family! We adopted our beautiful {pet's name} on {date}.";
saying[1] = "We're proud to announce the newest addition to our family... {pet's name}, adopted {date}. -{your name}";
saying[2] = "We've adopted our new pride and joy, {pet's name} on {date}. -{your name}";
document.forms[1].elements['Product_Attributes[6]:value'].value = 'Hi';
function setSaying(num, saying){
if(document.forms){
document.forms[0].elements['Product_Attributes[6]:value'].value = saying[num];
}
}
function getSaying(num, saying){
return saying[num];
}
</script>

<!-- This is for listing the sayings on the page /-->
<script language="javascript" type="text/javascript">
if(document.forms){
document.write('<ul style="list-style-type: none; font-size: 11px; font-family: verdana, arial, sans-serif;">');
for(i = 0; i < saying.length; i++){
document.write(' <li style="padding-top: 5px"><a href="merchant.mvc?Screen=PROD&Store_Code=MTP&Product_Code=ANNF#top" onclick="javascript:setSaying(' + i + ', saying);">' + (i+1) + '. </a>' + saying[i] + '</li>');
}
document.write('</ul>');
}
</script>
The error I am getting is document.forms[0] has no properties

I really really need to work on my javascript skills...


JayBird | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could have it search the forms for your field.

Code: Select all

var ta = false;
for(var i in document.forms)
{
  if (typeof document.forms[i].elements['Product_Attributes[6]:value'] != 'undefined')
  {
    ta = document.forms[i].elements['Product_Attributes[6]:value'];
    break;
  }
}
ta.value = 'Hi!';
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hmmm... now it's telling me document.forms has no properties

The page markup is AWEFUL because it's generated by a shopping cart that I didn't choose, but I Can't change it.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

move the script to onload event handler or after the form. By the time your script is executing, there's no form yet.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Try this, and see if it meets your needs. It's just a basic testing of the functionality you want, you'll have to modify it to suit your needs.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test Page</title>
<meta name="Generator" content="Alleycode HTML Editor">
<meta name="Description" content="Your description here...">
<meta name="Keywords" content="Your keywords here...">
</head>

<script language="javascript" type="text/javascript"> 

function changeText (formElement) {

  if (formElement.value == "0")
    document.testForm.display.value = "Too many cooks spoil the broth";
  else if (formElement.value == "1")
    document.testForm.display.value = "Many hands make light work";
  else if (formElement.value == "2")
    document.testForm.display.value = "Idle hands are the devil's workshop";
  else
    document.testForm.display.value = "Value not recongized, sorry";
}

</script> 

<body>

<form name="testForm">

<select name="firstTest" onchange="changeText(this);">
  <option value="-1">Please pick one</option>
  <option value="0">First Saying</option>
  <option value="1">Second Saying</option>
  <option value="2">Third Saying</option>
</select>
<br /><br />

<input type="radio" name="secondTest" value="0" onclick="changeText(this);">First Saying<br />
<input type="radio" name="secondTest" value="1" onclick="changeText(this);">Second Saying<br />
<input type="radio" name="secondTest" value="2" onclick="changeText(this);">Third Saying <br />
<br /><br /><br /><br />

<textarea name="display" rows="5" cols="40"></textarea>


</body>
</html>
Post Reply