PDA

View Full Version : Various Ways of Using The Selector



flame-d
09-17-2013, 09:31 AM
Hello people, I'm new to this forum. I was a member of stack overflow for a while, but the account eventually got suspended for what they thought was a mis-use of information - because of that harsh environment, I google'd an alternative. This seems like a brilliant one.

Basically, when it comes down to the crunch - I really do consider myself a Designer and not a Developer. Although my skills are adequate(especially now I've noticed I can code better than I can spell 'adequate' - I got there on the fifth attempt). So, anyway this post - Some of the ways you can use the Javascript/jQuery selector and one I'm not familiar with, at all.

so, we have the body and container elements;


<body>
<div id="container"></div>
</body>


and we need to manipulate the DOM, specifically the container ID;

#1
$('#container').append("The first insance");
#2
$('body > div').append("The Second Instance");
#3
$('body #container').append("The Third Instance");
#4
$('div#container').append"(The Final Instance");

OK, so you're probably thinking "There's more than that." ....:D.

#2 is structure dependant - the selector is assuming you have
<div id="container">The First Instance</div> directly below the body tag. If you do not, the selector will return the div that is.

So, we may or may not know that the variations are available because each one will suit the needs which the page requires and some can be a benefactor for speed.

but, what is this?


$('#'+VAR)

var is a stored variable of sorts. if you are familiar with this syntax, you can probably use your imagination to complete the variable.

anyway, thanks for reading. I'm usually a silent stalker of code, I pulled out something relatively mondane, making the effort for my introduction, 'yano?
Thank you.
.

flame-d
09-17-2013, 08:32 PM
Looking at this now, I thought this would be something like Grab all ID's for every div and append whatever the Variable has stored; on the condition the selector within the variable is relating with $(this) and not something linear.

what are your thoughts, I'd love to read them. :).

traq
09-17-2013, 10:37 PM
Welcome to DD.

After reading your post, I'm not sure if you actually have a question or not. If you do, please explain.

If you are looking to start an open-ended discussion, let me know and I can move your thread to the General Discussion forum.

jscheuer1
09-18-2013, 12:59 AM
With:



$('#'+VAR)

VAR cannot be a variable of sorts. It's either a string variable or it's not. If it is, it will select the element with that id if there is one. Say VAR = 'container', then it will select that div from your previous post. If VAR is not a string variable, there will either be an error or nothing will be selected. If it is a string variable and there is no element with that id, there will be no error and nothing will be selected. If there are more than one elements with that id, generally it will select the first. In some circumstances/browsers though it will throw an error. In other circumstances (regardless of browser) it might select all of them, these last two possibilities are rare.