SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with an if statement

    I have a form that I am using a javascript with and I am trying to make it so if someone checks a checkbox, it will do one thing and if it is left unchecked, it will do another. For some reason it keeps inserting the value of the first if statement whether it is checked or not.

    Can somebody please tell me what I need to do to make it print the first value if it is checked, and the second if it is left unchecked?

    Code:
      if (external = "yes") {
       form.external.value = 'target="_blank"';
      } else {
       form.external.value = '';
      }
     
    <input name="external" type="checkbox" value="yes" />
    John Saunders

  2. #2
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      if (external == "yes") {
       form.external.value = 'target="_blank"';
      } else {
       form.external.value = '';
      }
     
    <input name="external" type="checkbox" value="yes" />
    = is an assignment operator.
    == is a conditional operator.
    Of course, that's just my opinion. I could be wrong.

  3. #3
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mr. Brownstone,

    Thanks for your reply. I tried the == instead of = and it came up with a Javascript error "external is undefined". Any ideas?
    John Saunders

  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think itís target you need:
    Code:
      if (external == "yes") {
       form.target = '_blank';
      } else {
       form.target = '';
      }
    Of course, that's just my opinion. I could be wrong.

  5. #5
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mr. Brownstone,

    I'm using the following code to reprint the current text in a textbox along with a link if the user creates one through a seperate form. Here's the JS that reposts back to the textarea field:

    Code:
    var link = '<a href="url" '+form.external.value+'>Link</a>
    form.message.value = form.message.value+link;
    Can you tell me what I would need to change in the code I just posted, after changing the code in the if statements to form.target?

    Thanks!
    John Saunders

  6. #6
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var link = '<a href="url" target="'+form.target+'">Link</a>
    form.message.value = form.message.value+link;
    That should do it, I think.
    Of course, that's just my opinion. I could be wrong.


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
  •