By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

How to encapsulate a statement

Claus Mygind
100+
P: 571
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
Expand|Select|Wrap|Line Numbers
  1. td.innerHTML =    '<INPUT'+
  2.             ' type="text"'+ 
  3.             ' name="SEARCH"'+ 
  4.             ' id="SEARCH"'+
  5.             ' onKeyPress="if ( isEnter( event )) { onClick="psRequestData(this)" };"'+
  6.             ' />';
  7.  
example 2
Expand|Select|Wrap|Line Numbers
  1. var cInsert = 'onClick="psRequestData(this);"';
  2. td.innerHTML =    '<INPUT'+
  3.             ' type="text"'+ 
  4.             ' name="SEARCH"'+ 
  5.             ' id="SEARCH"'+
  6.             ' onKeyPress="if ( isEnter( event )) {'+cInsert+'};"'+
  7.             ' />';
  8.  
Jun 10 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
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;
Jun 10 '08 #2

Claus Mygind
100+
P: 571
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?
Jun 10 '08 #3

Claus Mygind
100+
P: 571
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.
Jun 10 '08 #4

acoder
Expert Mod 15k+
P: 16,027
The escape character is \, so you can escape single quotes like this:
Expand|Select|Wrap|Line Numbers
  1. \'
However, you can't set an onclick like you have in your code (in an if statement). Set it in the isEnter function instead.
Jun 10 '08 #5

Claus Mygind
100+
P: 571
Well how then would I code this line

Expand|Select|Wrap|Line Numbers
  1. ' 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.

Expand|Select|Wrap|Line Numbers
  1. ' onKeyPress="if ( isEnter( event )) { psRequestData(this) };"'
I really just want to execute the "psRequestData(this);" if the enter key was pressed.
Jun 10 '08 #6

Claus Mygind
100+
P: 571
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.
Jun 10 '08 #7

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad you got it working.
Jun 10 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.