Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 4 of 4

This is a discussion on What I am missing in this piece of code? in the Shared & Semi-Dedicated forum
Hello, I am trying to get an input text with a variable value, but I can't, here is the piece of code: <script type="text/javascript"> var1 ...

  1. #1
    JPC Member
    Join Date
    May 2003
    Posts
    28

    What I am missing in this piece of code?

    Hello,

    I am trying to get an input text with a variable value, but I can't, here is the piece of code:

    <script type="text/javascript">
    var1 = "Hello,%20World";
    var1 = unescape(var1);
    document.write(var1);
    document.write(<input type=text value=" +var1+ ">");
    </script>

    Line 3 is O.K., but the input text does not show the full string... What I am missing? or how can I achieve this?

    Regards,

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    First of all, in your second document.write() line you didn't include the opening quotes before "<input," but that's probably just a typo in your post.

    Is the field that is generated by the scriupt only showing "Hello" and not "world?" The reason is because you need to put quotes around the valve text in the input form. Otherwise the browser sees the value as Hello and sees "Word" as another attribute to the field (basically, the tag would look like <input type=text value=Hello Word>, where what you want it to look like is <input type=text value="Hello World">).

    Try changing that line to
    document.write("<input type=text value=\"" +var1+ "\">");
    and it should take care of your problem. When you place a backslash before the double quotes you tell JavaScript (or pretty much any language based on C syntax) to treat the quotes as literal quotation marks and not string delimiters (as they normally are), so that line will wrap Hello World in quotes and the whole thing will be displayed on the page.

    Hope this helps.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    JPC Member
    Join Date
    May 2003
    Posts
    28

    Thanks a lot!

    Thank you, Jason!

    You are always saving me with my little problems on programming...



    As I had to solve it as far as possible, I put the following code (and worked):

    document.form1.name.value = var1;

    Where form1 is the name of the form, name is name of the input text and var1 is the variable value.

    But thanks for your tip, next time I will remember...

    Greetings.

  4. #4
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Actually, IMHO, your "fix" is the better method to use. I was going to suggest it yesterday, but not knowing your JavaScript aptitude I didn't want to end up confusing you any more than you already would.

    There are two reasons why I feel this method is better:

    1. With your first method the page can't be fully displayed until all of the scripts have been run. The browser will start to render tha page, come to a block of code, stop rendering to run the code, "paste" in your generated html, then finish rendering. The second method causes the whole page to be rendered and then adds the values to the fields. Even though both methods will take about the same time to complete, the latter will look faster to a user with a slow connection.

    2. If a user has JavaScript turned off they won't see the form field in the first method. In the second they'll see the form and be able to submit it, they'll just need to type in the value themselves.

    With all modern browsers supporting at least some form of the document object model (DOM), which is what you're using when you use document.anything paths in JavaScript, there is really no need to ever use document.write() anymore.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •