PDA

View Full Version : document.getElemenyById("...") has no properties


Vlaad
05-15-2003, 02:22 AM
Hello People!

I'm having some trouble with a little bit of javascript. Surprise, surprise!

var tr = document.createElement('tr');

var producttd = document.createElement('td');
producttd.innerHTML = '<input name="ProductID'+rowCounter+'" type="hidden" id="ProductID'+rowCounter+'" value="">';

var rfqtd = document.createElement('td');
rfqtd.innerHTML = '<input name="RFQNumber'+rowCounter+'" readonly type="text" class="text" id="RFQNumber'+rowCounter+'" size="25" onClick="javascript:window.open(\'rfqselection.asp?row='+rowCounter+'&pid='+document.getElementById('ProductID'+rowCounter).value+rowCounter+'\',\'rfqselector\',\'height= 300,width=200,toolbar=no,status=no,scrollbars=no,resizable=no,menubar=no\'); return false;">';

tr.appendChild(producttd);
tr.appendChild(rfqtd);

There's more in the row, but here's the general idea. And yea, I know I probably should have made it more elegant by not using the innerHTML dodge-up, but I'm working to a deadline here, and I really don't see the point of adding an extra 30 lines of code (there's heaps more cells besides these two, but nevermind those).

Now... the error:
document.getElemenyById("ProductID"+rowCounter) has no properties.
Internet Explorer displays a lovely non-informative line of dribble.

Please help me get through this problem ASAP... it's freaking killing me!!!

Cheers in advance,
Vlaad

Vlaad
05-15-2003, 02:23 AM
Please keep in mind that the innerHTML stuff actually is on only 1 line (per). Hadn't considered how it'd be rendered on the forums...

HairyTeeth
05-15-2003, 02:27 AM
Do you need getElementById??? IF not, why not just do this:

row='+rowCounter+'&pid='ProductID'+rowCounter.

Vlaad
05-15-2003, 02:36 AM
it's a form object's value that I'm trying to retrieve. ProductID is the name of the form object, not the value of the object.

Anyone else? Batter up?!

<please???!!!>

cheesebagpipe
05-15-2003, 05:55 AM
I think your problem is: you want document.getElementById('ProductID3'), e.g., to run onclick of the field, not when the table is being assembled. Quotes can get messy here...see if this is any closer:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>hoohah</title>
</head>
<body>
<table id="vlaad">
<tbody>
</tbody>
</table>
<script type="text/javascript" language="javascript">

var rowCounter, click_handler, product_tr, product_td, rfqtd, rfqtd_input;
for (rowCounter=1; rowCounter<8; rowCounter++) {

product_tr = document.createElement('tr');
product_td = document.createElement('td');
product_input = document.createElement('input');
product_input.setAttribute('type', 'hidden');
product_input.setAttribute('name', 'ProductID' + rowCounter);
product_input.setAttribute('id', 'ProductID' + rowCounter);
product_input.setAttribute('value', 'ProductID' + rowCounter + 'demo'); //demo
rfqtd = document.createElement('td');
rfqtd_input = document.createElement('input');
rfqtd_input.setAttribute('type', 'text');
rfqtd_input.setAttribute('name', 'RFQNumber' + rowCounter);
rfqtd_input.setAttribute('id', 'RFQNumber' + rowCounter);
rfqtd_input.setAttribute('class', 'text');
rfqtd_input.setAttribute('size', '25');
rfqtd_input.setAttribute('readonly', 'readonly');
click_handler = new Function('window.open("rfqselection.asp?row='+rowCounter+'&pid="+document.getElementById(\\'ProductID'+rowCounter+'\\').value,"rfqselector","height=300,width=200")');
rfqtd_input.setAttribute('onclick', click_handler);
rfqtd.appendChild(rfqtd_input);
product_td.appendChild(product_input);
product_tr.appendChild(product_td);
product_tr.appendChild(rfqtd);
document.getElementById('vlaad').firstChild.appendChild(product_tr);

}

</script>
</body>
</html>

Vlaad
05-15-2003, 06:26 PM
Many thanks cheesebagpipe!

I actually managed to get it to work by abstracting the onClick's javacsript actions into a separate function. Basically what you've done here.

I didn't mention on the forum that I'd solved the problem, because, frankly, i was looking for a solution that was a tad more elegant. And you've really done it there!

The funny thing is that though I'd already been using the setAttribute calls everywhere, I never even vaguely considered setting the onClick attribute programatically.

Cheers for all your help!