I'm not going to go any further with this thread, All I said from the start was that it isn't in my best practices to write code as in the first post. IE: var myVar = "#testDiv";
We all know there are many ways of writing something. We don't need to get into the fine details of assigning a value to a var. To me assigning a value and or a string can be two separate things. Assignment such as: var html = '<em>[EMAIL PROTECTED]</em> Pizza or pasta?'; Will almost never be changed and I consider to be a static/final variable, which javascript doesn't have, where as an asignment of: var myVar = "#testDiv"; Is much more mutable and serves no purpose of having a # and read to me as poorly structured code. The above var assignments have totally different purposes. And yes, They are both valid assigments. I meant no harm towards hubbs coding abilities. From: Michael Geary <[EMAIL PROTECTED]> Subject: [jQuery] Re: Var and a child To: jquery-en@googlegroups.com Date: Monday, August 18, 2008, 9:18 PM 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.