The short answer is you don't have to use the type of element before the id
or class
name in the selector.
The long answer is you should never have to use the element name before an id
selector because id
s should always be unique per page. The only reason you would ever want to specify the element type before the id
is if you wanted to note to yourself what type of element you're dealing with.
When it comes to classes, however, you could most certainly be in a situation where you want to have one element of many elements with the same class name that has a certain behavior the others do not. An example of that would be something like this.
<script type="text/javascript">
$('a.button').click(function( ){
alert("I'm a link that looks like a button!");
return false;
});
$('input.button').click(function( ){
alert("I'm an input that looks like a button!");
return false;
});
</script>
<a href="#" class="button">Link</a>
<input type="submit" name="button" value="Submit Button" />
In summary: you're right to question selectors with the element type prepending the id
.
Hopefully that clears things up.
Note: My example implies there is some CSS to make the 2 elements look the same. That's the reason they share the same class name.