Web Analytics Made Easy -
StatCounter Hitting enter is not executing the search? - CodingForum

Announcement

Collapse
No announcement yet.

Hitting enter is not executing the search?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hitting enter is not executing the search?

    Hey everyone....Im working on a real basic image viewer and im finally making some headway on this project. It works if I click search but not if i hit enter....any ideas why? heres my code:
    Code:
    <html>
    
    <head>
    <script Language="JavaScript">
    
    function JSTest(){
    
    	if (document.form1.txtSearch.value != ''){
    		//document.imgTest.src = document.form1.txtSearch.value + ".jpg";
    		//document.getElementById("imgTest").src = document.form1.txtSearch.value + ".jpg";
    
    		var floatimg = document.getElementById('imgTest');
    		floatimg.src = document.form1.txtSearch.value + ".jpg";
    
    
    
    	}
    
    }
    </script>
    </head>
    <body>
    
    	<form name="form1">
    		<input type="text" name="txtSearch" ><br>
    		<input type="button" value="search"  onclick="JSTest();">
    		<hr>
    		<IMG NAME="imgTest" id="imgTest" src="" alt="">
    		<hr>
    	</form>
    
    </body>
    </html>

  • #2
    No ones got any idea? darn

    Comment


    • #3
      The problem is this is the incorrect forum.
      Moving to javascript forum.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

      Comment


      • #4
        No one has answered because this forum is for Java and JSP, not Javascript and HTML. This thread should be moved to the Javascript forum. If this was the JS forum, it would have been answered fairly quickly.

        In your button, onclick is only called "on click", meaning clicking the button. When you hit enter, it submits the form and does not click the button (not even a submit button if you have one). To solve this, add onsubmit="JSTest();" to your form tag.

        -Shane

        Comment


        • #5
          The real question is.... why should it? I don't see any code to handle keypress. Try the following:

          Code:
          function testclicky(e, form) {
              if (!e)
                  e = window.event;
              var key = e.keyCode || e.which;
              if (key == 13 || key == 3) {
                  document.getElementById('thebutton').click();
              }
          }
          Give your button an ID and put it in the script above. And of course, change your input field like so:

          Code:
          <input type="text" name="txtSearch" onkeypress="testclicky(event, this)" />
          Give it a whirl, see how that goes for you. Oh, and... validate your HTML... most of your tags are open.
          The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

          How to ask smart questions?

          Comment


          • #6
            Sorry about the wrong forum...im a total noob.

            Thanks for the direction guys I am testing this out right now....sorry for being annoying.

            Comment


            • #7
              Originally posted by Eldarrion View Post
              The real question is.... why should it? I don't see any code to handle keypress. Try the following:

              Code:
              function testclicky(e, form) {
                  if (!e)
                      e = window.event;
                  var key = e.keyCode || e.which;
                  if (key == 13 || key == 3) {
                      document.getElementById('thebutton').click();
                  }
              }
              Give your button an ID and put it in the script above. And of course, change your input field like so:

              Code:
              <input type="text" name="txtSearch" onkeypress="testclicky(event, this)" />
              Give it a whirl, see how that goes for you. Oh, and... validate your HTML... most of your tags are open.
              No offense, but triggering a keypress event for something this simple is seriously overkill. Why not do an onsubmit on the form like I suggested? If the button is changed to a submit button, it's all set and no onclick event is even needed.

              What if the user tabs to the button and clicks the spacebar? That will bypass your script and you'll then have to add the keycode 32 in there. And soon you're hacking the script to account for all ways the button can be clicked or form submitted.

              Always employ the KISS technique

              -Shane

              Comment


              • #8
                Originally posted by TheShaner View Post
                No offense, but triggering a keypress event for something this simple is seriously overkill. Why not do an onsubmit on the form like I suggested? If the button is changed to a submit button, it's all set and no onclick event is even needed.

                What if the user tabs to the button and clicks the spacebar? That will bypass your script and you'll then have to add the keycode 32 in there. And soon you're hacking the script to account for all ways the button can be clicked or form submitted.

                Always employ the KISS technique

                -Shane
                None taken, although.... an event bound onclick would work equally if the button is selected and the user presses the space bar or if the button is clicked via the mouse, so it should work none-the-less. I wasn't trying to say that your method is worse or anything... I was just suggesting an alternative that achieves the same result... the rest is really just semantics. In some cases, I wouldn't use onsubmit to do job... for instance if you need to get the result of the button's action before actually doing a submit, although that might work a bit weird in regards to the enter button handler, but eh... enough about that.
                The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

                How to ask smart questions?

                Comment

                Working...
                X