How to encapsulate a statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    How to encapsulate a statement

    I am creating an input field dynamically. I want to test to see if the user pressed the "Enter Key" to start processing. I seem to need 3 wrappers single quote ', double quote " and one more. Below are two attempts I have made to get around the limitation of only two delimiters. Neither works.

    How can I write the code below so it is properly delimited?

    example 1
    Code:
    td.innerHTML =	'<INPUT'+
    			' type="text"'+ 
    			' name="SEARCH"'+ 
    			' id="SEARCH"'+
    			' onKeyPress="if ( isEnter( event )) { onClick="psRequestData(this)" };"'+
    			' />';
    example 2
    Code:
    var cInsert = 'onClick="psRequestData(this);"';
    td.innerHTML =	'<INPUT'+
    			' type="text"'+ 
    			' name="SEARCH"'+ 
    			' id="SEARCH"'+
    			' onKeyPress="if ( isEnter( event )) {'+cInsert+'};"'+
    			' />';
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The problem is not just limited to delimiters (though that can be solved by escaping). Are you trying to set an onclick if enter is pressed? You'd be better off setting this in a function using txtbox.onclick = someFunction;

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Yes I do have a function to actually handling the event when a key is pressed on the keyboard. I use that function other places and is not the source of my problem here.

      The problem here is I am trying to write this input element dynamically (on the fly) and I seem to be using up the available delimiters.

      Can you give an example of how the quotes would be escaped?

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        I should add, I want to see when and if the user is pressing the "Enter Key". So we are really testing every key stroke. I already have a "Go" button with the onClick event handler. This is a short cut which will enable the same action without the user leaving the input field.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          The escape character is \, so you can escape single quotes like this:
          Code:
          \'
          However, you can't set an onclick like you have in your code (in an if statement). Set it in the isEnter function instead.

          Comment

          • Claus Mygind
            Contributor
            • Mar 2008
            • 571

            #6
            Well how then would I code this line

            Code:
            ' onKeyPress="if ( isEnter( event )) { onClick="psRequestData(this)" };"'
            Perhaps what you are telling me the "onClick" is really not needed here. And it should be written like this.

            Code:
            ' onKeyPress="if ( isEnter( event )) { psRequestData(this) };"'
            I really just want to execute the "psRequestData( this);" if the enter key was pressed.

            Comment

            • Claus Mygind
              Contributor
              • Mar 2008
              • 571

              #7
              Yep! that was the solution. I had copied the line from the Button's onClick event handler and forgotten to remove it in the onKeyPress event handler for the the text element.

              Thanks for the help.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You're welcome. Glad you got it working.

                Comment

                Working...