Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31832

Re: How to deal with "messy" quotes?

From <cyber@example.com>
Subject Re: How to deal with "messy" quotes?
Newsgroups comp.lang.javascript
References (1 earlier) <zuB3A.371253$kM7.70526@fx44.am4> <da86b656-3c71-4d67-bb56-26661b51ab61@googlegroups.com> <VOB3A.181572$eh.139780@fx08.am4> <5d7849ff-803f-44bf-a64a-f51f7df741ab@googlegroups.com> <71de1dbd-1779-4131-8c68-4c9d6e01d774@googlegroups.com>
Message-ID <FfR3A.215307$223.44894@fx20.am4> (permalink)
Organization free.xsusenet.com
Date 2016-12-13 11:49 +0000

Show all headers | View raw


justaguy <lichunshen84@gmail.com> wrote:
> On Monday, December 12, 2016 at 5:10:17 PM UTC-5, justaguy wrote:
>> On Monday, December 12, 2016 at 1:15:22 PM UTC-5, cy...@example.com wrote:
>> > justaguy <> wrote:
>> > > On Monday, December 12, 2016 at 12:53:40 PM UTC-5, cy...@example.com wrote:
>> > >> justaguy <> wrote:
>> > >> > 
>> > >> > HTML:
>> > >> > <html>
>> > >> > <body>
>> > >> > <span id="nav"></span>
>> > >> > </body>
>> > >> > </html>
>> > >> > 
>> > >> > JavaScript:
>> > >> > <script>
>> > >> > 
>> > >> > var s = document.getElementById('nav'),
>> > >> >         num = 0,
>> > >> >         fn = " a new field name ";      
>> > >> > /* in my actual code, the var s represent an element dynamically created
>> > >> > but to get key point across I'm using static element here
>> > >> > */  
>> > >> > s.innerHTML = "&nbsp;" + fn +  " <input type='button' value='x' onclick = 'newFN("+fn+" + "," +num+);'>";
>> > >> > 
>> > >> > var newFN = function(f,n) {
>> > >> >         console.log(f,n);
>> > >> >         }       
>> > >> >         
>> > >> >  </script>
>> > >> >  
>> > >> >  Intention:
>> > >> >  for the s.innerHTML to have a button, with a "+" value/sign, upon click of which, it loads a function named "newFN" with two parameters
>> > >> > 
>> > >> >  Problem:
>> > >> >  Not sure to how to manage the quotes (both singles and doubles) for the s.innerHTML line, the right side of the equation.
>> > >> >  
>> > >> >  Thanks.
>> > >> 
>> > >> This should work:
>> > >> 
>> > >> <html>
>> > >> <body>
>> > >> <span id="nav"></span>
>> > >> <script>
>> > >> 
>> > >> var s = document.getElementById('nav'),
>> > >>   num = 0,
>> > >>   fn = " a new field name ";
>> > >> /* in my actual code, the var s represent an element dynamically created
>> > >> but to get key point across I'm using static element here
>> > >> */
>> > >> s.innerHTML = "&nbsp;" + fn +  " <input type='button' value='x' onclick = 'newFN(\""+fn+" + \", \"+num+\");'>";
>> > >> 
>> > >> var newFN = function(f,n) {
>> > >>   console.log(f,n);
>> > >>   }
>> > >> 
>> > >>  </script>
>> > >> 
>> > >> </body>
>> > >> </html>
>> > > 
>> > > Getting close except that the num var value has not been passed down.  And since it's a number we don't need quotes for it as a parameter value.  Thanks.
>> > > 
>> > 
>> > This should pass the num variable:
>> > s.innerHTML = "&nbsp;" + fn +  " <input type='button' value='x' onclick = 'newFN(\""+fn+" + \", "+num+");'>";
>> 
>> Somehow I missed your response.
>> 
>> Yeah, it does, but with the + sign in front of it.  And I removed it, so, the code is now:
>> s.innerHTML = "&nbsp;" + fn +  " <input type='button' value='x' onclick = 'newFN(\""+fn+" \", "+num+");'>";
>> 
>> It works perfectly.  Thank you.
> 
> Ok, now I have a new requirement, that is, to add an ID to the button.
> the value of the ID is made of a string and a number.
> 
> The following code still hasn't escaped some quotes.  How to fix it?  Thanks.
> 
> var s2;
> s2 = "&nbsp;" 
>      + fn +  
>      " <input type='button' id="NewEl"+num value='x' onclick = 'newFN(\""+fn+"\" + "+num+");'>"; 

This should work:

s2.innerHTML = "&nbsp;"
  + fn +
	" <input type='button' id='NewEl"+num+"' value='x' onclick = 'newFN(\""+fn+"\" + "+num+");'>";

If NewEl is meant to be the string "NewEl" then you need to put it between quotes or else it will be be
interpreted as a variable.
And num and the + operator should be outside the quotes since num is meant to be a variable and +
is meant to be the concatenation operator, but if you put them between quotes they will just be
seen as being the strings "num" and "+" respectively.

BTW the s2 button would call newFN while leaving n undefined so that parameter could be removed.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-11 16:23 -0800
  Re: How to deal with "messy" quotes? JJ <jj4public@vfemail.net> - 2016-12-12 12:42 +0700
    Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-12 09:56 -0800
      Re: How to deal with "messy" quotes? JJ <jj4public@vfemail.net> - 2016-12-13 18:18 +0700
        Re: How to deal with "messy" quotes? JJ <jj4public@vfemail.net> - 2016-12-13 18:22 +0700
          Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-13 13:10 +0100
    Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-12 20:43 +0100
  Re: How to deal with "messy" quotes? <cyber@example.com> - 2016-12-12 17:53 +0000
    Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-12 10:05 -0800
      Re: How to deal with "messy" quotes? <cyber@example.com> - 2016-12-12 18:15 +0000
        Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-12 14:10 -0800
          Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-12 16:56 -0800
            Re: How to deal with "messy" quotes? <cyber@example.com> - 2016-12-13 11:49 +0000
              Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-13 13:17 +0100
                Re: How to deal with "messy" quotes? <cyber@example.com> - 2016-12-13 18:32 +0000
                Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-13 19:47 +0100
                Re: How to deal with "messy" quotes? <cyber@example.com> - 2016-12-13 19:43 +0000
                Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-14 12:24 +0100
              Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-13 17:07 -0800
    Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-12 21:19 +0100
      Re: How to deal with "messy" quotes? justaguy <lichunshen84@gmail.com> - 2016-12-12 12:51 -0800
  Re: How to deal with "messy" quotes? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-12-12 20:50 +0100

csiph-web