Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > html form in javascript variable

Reply
Thread Tools

html form in javascript variable

 
 
mirek
Guest
Posts: n/a
 
      11-12-2009
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-13-2009
mirek wrote:

> 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);
}


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-13-2009
Martin Honnen wrote:

> 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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-13-2009
Thomas 'PointedEars' Lahn wrote:

> Martin Honnen wrote:
>> 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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
mirek
Guest
Posts: n/a
 
      11-16-2009
On 13 Lis, 19:26, Martin Honnen <(E-Mail Removed)> wrote:
> mirek wrote:
> > 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);
>
> }
>
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


Thank U very much. InnerHtml is the answer 4 my question
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-16-2009
mirek wrote:

> On 13 Lis, 19:26, Martin Honnen <(E-Mail Removed)> wrote:
>> 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
When JavaScript is disabled, do not display HTML form and form elements patricksabourin@hotmail.com Javascript 5 08-10-2006 05:32 AM
passing a javascript variable into an html form element michael Javascript 3 11-15-2005 11:56 PM
Post Form with Variable of the form itself TNG HTML 2 03-03-2005 05:34 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments