0

Let's say I have couple of web pages where a single datepicker is used. In addition, I have another web page where I need 3 datepickers on the same page.

So how do I manage the java script id reference? Should I have single script in _layout.xml with something like this?

<script>
        $(document).ready(function () {            
            $("#datepicker1").datepicker();
            $("#datepicker2").datepicker();
            $("#datepicker3").datepicker();
        });
    </script>

And all pages (besides the multiple) will have :

 <div id="datepicker1"></div>

and the multiple will have:

 <div id="datepicker1"></div>
 <div id="datepicker2"></div>
 <div id="datepicker3"></div>

I come from OO programming and something doesn't fit here. What are the conventions in web\js world to this common use case?

Edit: the mmultiple datepicker page script has different settings...

1
  • It's possible to grab multiple IDs in jQuery with $('[id^="datepicker"]').datepicker(), but as has been noted below, the best solution is a common class. Commented Jan 16, 2014 at 16:50

2 Answers 2

1

It depends. If they all use exactly the same settings, use a class:

<div class="datepicker"></div>

then

$('.datepicker').datepicker();

That will handle everything with the class datepicker on it, no matter how many there are (one, three, ten thousand [though that might make the page incredibly slow]).

If they need different settings then you'll need to stick with IDs, and initialise them all individually, as you've shown in the question.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the answers in this thread. I have decided to go with common class to all and for multiple (which has different settings) I'll create its own thing. cheers!
0

I would use a class instead of an ID if they are all going to initializate the plugin in the same way:

<script> 
$(document).ready(function () {
    $(".datepicker").datepicker();
}); 
</script>

HTML

<div class="datepicker"></div>
<div class="datepicker"></div>
<div class="datepicker"></div>

7 Comments

This is what I do too. But here is what I don't like about it: ReSharper and Visual Studio warn me that the class is not defined anywhere. I could always put extra markup in my CSS, but that seems like the exact wrong answer to me. Has anyone else solved this problem?
@CharlieKilian It's a warning, not an error, so feel free to ignore it; as far as I'm aware/concerned using classes explicitly for identifying elements (i.e. the class has no attached styling) is a standard practice.
@CharlieKilian I don't see any problem with it. Those warnings aren't suppose to be there. This is technically the good way to do it.
Agree with you both. I hate ignoring warnings, is the only thing. It leads to a bad mindset. But thank you both for your feedback. Good to know I'm not the only one.
@CharlieKilian are you saying that sometimes the .datepicker won't be in the HTML markup while the javasript calling it will?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.