tags:

views:

34

answers:

4

Hello

I am trying to set text when loading a php file... I am doing somthing like this:

<?php echo tep_draw_textarea_field('enquiry', 'soft', 50, 15); ?>
<?php 
echo "<script> 
var a = document.getElementsByName('enquiry'); 
a.value='Pedido de Informação de Preço, relativamente ao producto: http://www.musicland.pt/musicstore/product_info.php?products_id=2581';
</script>"; ?>

Now I know that this is silly but is just a text. I want to do an evaluation first and if there is and id then fill the text area

the source generated have it correct and there is no javascript errors but the textarea stills empty.

I don't understand why!?

Can someone help me out on this

+1  A: 

getElementsByTagName returns a live node list. If your element has an ID, use getElementById. If you want to do this on multiple elements in the node list, use a for loop.

var a = document.getElementById('enquiry'); 
meder
In your current code, assuming you only have (or want to access) one textarea, you could use `a[0].value` instead of `a.value`
Ryan Kinal
+1  A: 

getElementsByName() returns an array of all the elements with the specified name. If your text-area is the first element on the page with the name "enquiry", you can add [0] to select it from the array. Second of all you should never EVER use single-character variable names. They are much harder to understand and work with. This is a more readable version of your code that should work:

var enquiryTextArea = document.getElementsByName('enquiry')[0]; 
a.value="blah blah blah";

enquiryTextArea tells you exactly what the variable means. Its the enquiry text-field itself. Not an array. If you care about code size, you can use a javascript minifier.

Better still, you can use getElementById() as the name attribute is deprecate on non-form related tags and is better when selecting one specific element:

var enquiryTextArea = document.getElementById('enquiry'); 
a.value="blah blah blah";

This is going to sound really typical, but firebug for firefox can be a HUGE help when diagnosing problems like these, as can jquery when doing absolutely anything with javascript. :D

CrazyJugglerDrummer
A: 

Use:

var a = document.getElementsByName('enquiry')[0]; 

And it should work. But I agree with meder's comment. If you have more than one textarea with the same name and you want to change all of them, you should put this in a for loop, like this:

var a = document.getElementsByName('enquiry'); 
for( var i=0; i<a.length; i++)
     a[i].value = "Some Text";
Fabiano
A: 
var a = document.getElementsByName('enquiry'); 

is empty until after the page loads.

in your tag, put an onLoad event, and make your code a function that onLoad calls.

echo "<script> 
function foo() {
    var a = document.getElementsByName('enquiry'); 
    a.value='Pedido de Informação de Preço, relativamente ao producto: http://www.musicland.pt/musicstore/product_info.php?products_id=2581';
}
</script>"; 

And then in the body tag :

<body onLoad="foo();">
jason