Ripple, I'm really sorry, but you seem to be mixing up some distinct and
unrelated concepts. Bear with me and I'll explain...
 
> I would never pass a variable with a string that started
> with anything other than an alpha-numeric value and
> have never see that done either.
> 
> I would never write
> 
> var string1 = "#This is string1";
> var string2 = "$This is string2";

 
There's nothing conceivably wrong with either of those strings, any more
than there is with this one:
 
var html = '<em>[EMAIL PROTECTED]</em> Mike is an idiot';
 
Honest! Ask any JavaScript expert if you don't believe me.
 
There are some special characters in JavaScript strings, but # and $ and <
are not among them.
 
Are you confusing string literals with variable names? They don't follow the
same rules, nor should they.
 
#, of course, is not a valid character in JavaScript variable names at all.
But $ is, as illustrated by its use in jQuery and in code like this:
 
    var $test = $('#test'), test = $test[0];
    // $test is a jQuery object, test is the DOM element
    alert( $test.attr('id') );  // alert 'test'
    alert( test.id );  // alert 'test'
 
This use of the $ prefix in variables holding references to jQuery objects
is considered a "best practice" in jQuery code.
 
> <div class=".class" id="#id"></div>
 
Well, of course that's invalid! '#' is not a valid character in an HTML id
attribute:
 
http://www.w3.org/TR/html401/types.html#type-name
 
But the '.' in a class attribute is perfectly legal, and has some very
legitimate purposes. If you're interested I can explain in more detail, but
it deserves a full description and a working example. (Hint: '.' is not
valid in a CSS classname selector because it delimits those selectors, but
it is valid in HTML, and the W3C recommends the use of the class attribute
as a way to pass arbitrary data to user agent code such as JavaScript.)
 
-Mike



  _____  

From: ripple



I am just saying that I would never pass a variable with a string that
started with anything other than an alpha-numeric value and have never see
that done either.
 
I would never write
 
var string1 = "#This is string1";
var string2 = "$This is string2";
 
or 
 
<div class=".class" id="#id"></div>
 
A lot of languages use some type of non-alpha-numeric character, as part of
it's structure so I would never try to mix them. Other people have to read
and work with that code also and it could get confusing and/or misleading.

From: Michael Geary


I'm afraid I don't follow you. We were talking about this line of code:
 

var myVar = "#testDiv";

 
That looks like your basic string assignment statement to me.
 
What is ugly about it, and how does it goes against your best practices?
 
Thanks,
 
-Mike



  _____  

From: ripple



Ok, It's in my best practices, not to do something like that. It's just ugly
coding.
 
But if your ok with it, then you run with it.


From: Michael Geary


The # you're referring to:
 
var myVar = "#testDiv";

is part of a string literal, not part of a variable name, so it's perfectly
legal.



  _____  

From: ripple



You can not declare a javascript var with a # as first char.
 
try:
 
var myVar = "testDiv";
$('#'+myVar+' .statusColor).css("background-color","#DBF18A");

Other than that it should work.

--- On Sun, 8/17/08, hubbs <[EMAIL PROTECTED]> wrote:


From: hubbs


I am trying to var to work as a selector along with an additional

class, but I must have something wrong.



var myVar = "#testDiv";

$(myVar

".statusColor").css("background-color","#DBF18A");



<div id="testDiv">

  <span class="statusColor">Test</span>

</div>



I need to select the child of the myVar, but the selector seems to be

incorrect.




Reply via email to