html form in javascript variable

M

mirek

Hi,
My problem is:
I have javascript variable like this:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
... <form>";
How can I get nameABC and put it into some other variable without
reloading a browser?
Thanks
 
M

Martin Honnen

mirek said:
I have javascript variable like this:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
... <form>";
How can I get nameABC and put it into some other variable without
reloading a browser?

What is it exactly that you want to read out? The id attribute value of
the first input element in the form?

That could be done as follows:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
"</form>";

var div = document.createElement('div');
div.innerHTML = variable;

var input =
div.getElementsByTagName('form')[0].getElementsByTagName('input')[0];
if (input != null) {
alert(input.id);
}
 
T

Thomas 'PointedEars' Lahn

Martin said:
What is it exactly that you want to read out? The id attribute value of
the first input element in the form?

That could be done as follows:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
"</form>";

var div = document.createElement('div');
div.innerHTML = variable;

var input =
div.getElementsByTagName('form')[0].getElementsByTagName('input')[0];
if (input != null) {
alert(input.id);
}

Given a Valid document, that is unnecessarily complicated and error-prone.
(You know better than that.)

window.alert(document.forms["formABC"].elements["nameABC"].id);

or

window.alert(document.getElementById("nameABC").id);

Not that the latter reference worm is recommended, though. All return
values should be tested before being used.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Martin said:
What is it exactly that you want to read out? The id attribute value of
the first input element in the form?

That could be done as follows:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
"</form>";

var div = document.createElement('div');
div.innerHTML = variable;

var input =
div.getElementsByTagName('form')[0].getElementsByTagName('input')[0];
if (input != null) {
alert(input.id);
}

Given a Valid document, that is unnecessarily complicated and error-prone.
(You know better than that.)

window.alert(document.forms["formABC"].elements["nameABC"].id);

or

window.alert(document.getElementById("nameABC").id);

Not that the latter reference worm is recommended, though. All return
values should be tested before being used.

Sorry, I have misunderstood your answer. OP: Use my answer as a solution if
you want to find out the value of the contro instead.


PointedEars
 
M

mirek

mirek said:
I have javascript variable like this:
var variable = "<form name='formABC'  method='POST'>"+
                 "<input type='text' id='nameABC'>"+
                 ... <form>";
How can I get nameABC and put it into some other variable without
reloading a browser?

What is it exactly that you want to read out? The id attribute value of
the first input element in the form?

That could be done as follows:

var variable = "<form name='formABC'  method='POST'>"+
                  "<input type='text' id='nameABC'>"+
                 "</form>";

var div = document.createElement('div');
div.innerHTML = variable;

var input =
div.getElementsByTagName('form')[0].getElementsByTagName('input')[0];
if (input != null) {
   alert(input.id);

}

Thank U very much. InnerHtml is the answer 4 my question :)
 
T

Thomas 'PointedEars' Lahn

mirek said:
What is it exactly that you want to read out? The id attribute value of
the first input element in the form?

That could be done as follows:

var variable = "<form name='formABC' method='POST'>"+
"<input type='text' id='nameABC'>"+
"</form>";

var div = document.createElement('div');
div.innerHTML = variable;

var input =
div.getElementsByTagName('form')[0].getElementsByTagName('input')[0];
if (input != null) {
alert(input.id);

}
[...]

Thank U very much. InnerHtml is the answer 4 my question :)

You mistyped "the cause of my future problems" only slightly.

This is not an Internet chat. And trim your quotes.

<http://jibbering.com/faq/#posting>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top