Can multiple ids be handled like in the code?
<script> $("#segement1, #segement2, #segement3").hide() </script> <div id="segement1"/> <div id="segement2"/> <div id="segement3"/>
Yes, #id selectors combined with a multiple selector (comma) is perfectly valid in both jQuery and CSS.
The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.
You could implement your own function that takes multiple ids and returns multiple elements. You could use document. querySelectorAll() that allows you to specify multiple ids in a CSS selector string . You could put a common class names on all those nodes and use document.
While you can give a div multiple ids, only one will be applied by the browser. The second id or any other id after the first one will just be ignored by the browser. So having multiple ids for a div is not only pointless but is also incorrect HTML.
Yes, #id
selectors combined with a multiple selector (comma) is perfectly valid in both jQuery and CSS.
However, for your example, since <script>
comes before the elements, you need a document.ready
handler, so it waits until the elements are in the DOM to go looking for them, like this:
<script> $(function() { $("#segement1,#segement2,#segement3").hide() }); </script> <div id="segement1"></div> <div id="segement2"></div> <div id="segement3"></div>
To your secondary question
var elem1 = $('#elem1'), elem2 = $('#elem2'), elem3 = $('#elem3');
You can use the variable as the replacement of selector.
elem1.css({'display':'none'}); //will work
In the below case selector is already stored in a variable.
$(elem1,elem2,elem3).css({'display':'none'}); // will not work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With