Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How disable refresh when enter key pressed in the form input text?

How disable refresh when enter key pressed in the form input text?

Scheduled Pinned Locked Moved JavaScript
helpjavascripthtmlquestion
7 Posts 4 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flashery
    wrote on last edited by
    #1

    Anybody knows how disable refresh when enter key pressed in the form input text in JavaScript? I have this JavaScript code

    // JavaScript Document
    function runScript(e){
    if(e.keyCode == 13){
    var div = document.createElement("div");
    div.setAttribute("class", "resultItem");
    var parentDiv = document.getElementById("result");
    var input = document.getElementById("txtBox");
    div.innerHTML = input.value;
    parentDiv.appendChild(div);
    }
    }

    My HTML code:

    Untitled Document

    Website URL:
    

    URL List

    My problem is when everytime I hit enter on my keyboard it refresh the page. I want it in every enter the value in the text box will be displayed below the URL List. Any help will be appreciated

    S 1 Reply Last reply
    0
    • F flashery

      Anybody knows how disable refresh when enter key pressed in the form input text in JavaScript? I have this JavaScript code

      // JavaScript Document
      function runScript(e){
      if(e.keyCode == 13){
      var div = document.createElement("div");
      div.setAttribute("class", "resultItem");
      var parentDiv = document.getElementById("result");
      var input = document.getElementById("txtBox");
      div.innerHTML = input.value;
      parentDiv.appendChild(div);
      }
      }

      My HTML code:

      Untitled Document

      Website URL:
      

      URL List

      My problem is when everytime I hit enter on my keyboard it refresh the page. I want it in every enter the value in the text box will be displayed below the URL List. Any help will be appreciated

      S Offline
      S Offline
      Shahriar Iqbal Chowdhury Galib
      wrote on last edited by
      #2

      hi,

      flashery wrote:

      <input id="txtBox" type="url" önKeyPress="return runScript(event)"/>

      Try this one

      <input id="txtBox" type="url" onkeydown="javascript:return runScript(event)" onkeypress="javascript:return false;"/>

      F 1 Reply Last reply
      0
      • S Shahriar Iqbal Chowdhury Galib

        hi,

        flashery wrote:

        <input id="txtBox" type="url" önKeyPress="return runScript(event)"/>

        Try this one

        <input id="txtBox" type="url" onkeydown="javascript:return runScript(event)" onkeypress="javascript:return false;"/>

        F Offline
        F Offline
        flashery
        wrote on last edited by
        #3

        I cant type anything on my textbox.. Maybe its because of this onkeypress="javascript:return false;"

        T 1 Reply Last reply
        0
        • F flashery

          I cant type anything on my textbox.. Maybe its because of this onkeypress="javascript:return false;"

          T Offline
          T Offline
          twseitex
          wrote on last edited by
          #4

          may be for oncontextmenu: window.event.returnValue=false; // result not a return return false; // oncontextmenu may be visible so for other events may be too (ignore of return value). if possible .... read out an event if(window.event!=null) {X01=window.event; if(X01.srcElement!=null) {X02=X01.srcElement; d010[0]=X02; if(X02.tagName!=null){d010[1]=X02.tagName;} d010[2]=X01.returnValue; d010[3]=X01.type; d010[4]=X01.cancelBubble; d010[5]=X01.reason; d010[6]=X01.propertyName; X00=true; } } save cancelBubble i.e. fireEvent() set cancelBubble allways to false X03=window.event.cancelBubble; X04=document.createEventObject(); X02=X00.fireEvent(X01,X04); window.event.cancelBubble=X03; Syntax bFired = object.fireEvent(sEvent [, oEventObject]) Parameters sEvent Required. String that specifies the name of the event to fire. oEventObject Optional. Object that specifies the event object from which to obtain event object properties. erzeugt mit document.createEventObject() Return Value Boolean. Returns one of the following values: true Event fired successfully. false Event was cancelled function fnFireEvents() { oDiv.innerText = "The cursor has moved over me!"; oButton.fireEvent("onclick"); }

          Using the fireEvent method

          By moving the cursor over the DIV below, the button is clicked.

          Mouse over this!

          Button

          F 1 Reply Last reply
          0
          • T twseitex

            may be for oncontextmenu: window.event.returnValue=false; // result not a return return false; // oncontextmenu may be visible so for other events may be too (ignore of return value). if possible .... read out an event if(window.event!=null) {X01=window.event; if(X01.srcElement!=null) {X02=X01.srcElement; d010[0]=X02; if(X02.tagName!=null){d010[1]=X02.tagName;} d010[2]=X01.returnValue; d010[3]=X01.type; d010[4]=X01.cancelBubble; d010[5]=X01.reason; d010[6]=X01.propertyName; X00=true; } } save cancelBubble i.e. fireEvent() set cancelBubble allways to false X03=window.event.cancelBubble; X04=document.createEventObject(); X02=X00.fireEvent(X01,X04); window.event.cancelBubble=X03; Syntax bFired = object.fireEvent(sEvent [, oEventObject]) Parameters sEvent Required. String that specifies the name of the event to fire. oEventObject Optional. Object that specifies the event object from which to obtain event object properties. erzeugt mit document.createEventObject() Return Value Boolean. Returns one of the following values: true Event fired successfully. false Event was cancelled function fnFireEvents() { oDiv.innerText = "The cursor has moved over me!"; oButton.fireEvent("onclick"); }

            Using the fireEvent method

            By moving the cursor over the DIV below, the button is clicked.

            Mouse over this!

            Button

            F Offline
            F Offline
            flashery
            wrote on last edited by
            #5

            Thanks man but this code is really to advance for me> I'm just learning JavaScript. But thanks anyway.. How about this code

            function disableRefresh(e){
            if(e.keyCode == 13){
            return false;

                  //some code follow here
             }
            

            }

            this makes the following code after

            return false;

            execute without refreshing the page and it work for me. My html textbox is look like this.

            /form>

            Correct me if I'm wrong cause this is the best and simple way for me to do this.

            T 1 Reply Last reply
            0
            • F flashery

              Thanks man but this code is really to advance for me> I'm just learning JavaScript. But thanks anyway.. How about this code

              function disableRefresh(e){
              if(e.keyCode == 13){
              return false;

                    //some code follow here
               }
              

              }

              this makes the following code after

              return false;

              execute without refreshing the page and it work for me. My html textbox is look like this.

              /form>

              Correct me if I'm wrong cause this is the best and simple way for me to do this.

              T Offline
              T Offline
              twseitex
              wrote on last edited by
              #6

              disableRefresh(event) // param event must have type of object event // disableRefresh(this) give the pointer of INPUT that have an event like onkeypress

              H 1 Reply Last reply
              0
              • T twseitex

                disableRefresh(event) // param event must have type of object event // disableRefresh(this) give the pointer of INPUT that have an event like onkeypress

                H Offline
                H Offline
                Ha Duy Thu n
                wrote on last edited by
                #7

                add a input type text belowe it. demo: u want disable refesh when enter key pressed on input name 'a'

                Now u can press enter key and page will not reload

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups