QUcik Javascript question

fender

Limp Gawd
Joined
Mar 2, 2003
Messages
417
A thank you goes out to everyone who has answered so far. Here's my latest question:

I want to insert a string into a cell but the string has a variable in it. How would I do so with a string like the one below.

Code:
innerHTML = <input type="text" name="textX" size="40" />

The letter X would be replaced with the current value of a counter variable. I think something like the following would work, but I would rather do it differently.
Code:
		var element = document.createElement('input');
		element.setAttribute('type', 'text');
		element.setAttribute('name', 'text' + countervariable);
		element.setAttribute('size', '40');

Secondly, I've noticed that with IE, an onclick operation can only be performed once the target has finished loading while it doesn't matter with firefox. I'm guessing this is normal and cannot be corrected in any way?
 
<form name="form1" enctype="multipart/form-data" action="page.php" method="post">

<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="xxx" value="" />

</form>

<script language = javascript>

var variable = contents;
document.form1.xxx.value = variable;

</script>
 
Define a function, and set that function as the onClick() event for the button, like so:

Code:
<script language = javascript>
<!--

var variable = contents;

function setContents() {
    document.getElementById("variable").value = variable;
    return true;
}

-->
</script>

Then modify the html like so:

Code:
<form enctype="multipart/form-data" action="page.php" method="post">
    <input type="submit" name="submit" value="Submit" onClick="setContents();" />
    <input type="hidden" name="variable" value="contents" />
</form>

I would recommend changing either the javascript variable or the hidden input's name. It could get confusing if they are the same.
 
somebody's always gotta do it better LoL.

I havnt done any js in a long time.
 
Thanks to both of you. While I'm at it, how would you output html code like so?
Code:
var textNode = document.createTextNode('I want <b>this</b> in bold.');
cellRight.appendChild(textNode);
 
I occasionally had to do that sort of thing, and I thought it best to use a bunch of empty DIVs (already built into the HTML).

If you had:

<div id="div1"></div>

You would use something like the following to insert HTML into it:

document.getElementById("div1").InnerHTML = "I want <b>this</b> in bold.";

You can then of course do all kinds of style manipulations to make it look and behave the way you want it to.
 
Ack! Confusing!

It may be best to start new threads for new questions.

As for creating new elements by using JS object constructors, I don't know. I've never been that into dynamic content on the client side.
 
I didn't want to clutter the forum with new javascript questions every few hours so I though it would be best to house them in one thread.

Basically what I need is the javascript equivalent of this line in PHP.

echo '<input type="text" name="text' . $variable . '" size="40" />';

innerHTML needs to be used by the way.
 
fender said:
I didn't want to clutter the forum with new javascript questions every few hours so I though it would be best to house them in one thread.

Basically what I need is the javascript equivalent of this line in PHP.

echo '<input type="text" name="text' . $variable . '" size="40" />';

innerHTML needs to be used by the way.

to get the value of that field:

theVal = document.getElementById("text" + var).value;
 
maw said:
to get the value of that field:

theVal = document.getElementById("text" + var).value;

Wouldn't this work?

var name = 'text' + variable
innerHTML = '<input type="text" name="' + name + '" size="40" />'
 
fender said:
Wouldn't this work?

var name = 'text' + variable
innerHTML = '<input type="text" name="' + name + '" size="40" />'

ya, but that's going to be a bitch to debug. why do you need to generate form fields using javascript?
 
Back
Top