Click here to Skip to main content
15,888,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Alright so, this is how my looks like:

http://puu.sh/jBcjs/f379e46bf0.png[^]

I have a script so when I check the checkbox my Datum: frield will automatically get updated if I check more than 1 date it will looks like this:

YYMMDD YYMMDD YYMMDD

But the problem is, I can't choose any date.
If I want to choose any date at all I'll need to select the first date first, and than the other dates.

So this doesn't work:
http://puu.sh/jBcoI/286fd5b4b4.png[^]

But if I select the first date it works as can be seen here:
http://puu.sh/jBcq8/d5b1bac434.png[^]

this is my script.

JavaScript
function SetDate(dt) {
       if(document.getElementById("isoDate").checked) {
           if(dateArr.indexOf(dt)<0){ //Check if date is already added. if not add it to array.
              dateArr.push(dt);
        }
        else{
             var index=dateArr.indexOf(dt);
             if (index > -1) {
                dateArr.splice(index, 1);//Remove from array if checkbox uncheck
        }
      }
         $('#date').val(dateArr.join(" ")); //Add space separated string to the #date element.
     }
    }



And this is my view where I get the dates


HTML
@foreach (var date in ViewBag.MissingDays)
                                            {
                                                var isoDate = date.ToString("yyMMdd");
                                                <div class="col-md-1">
                                                    <input type="checkbox" id="isoDate" name="isoDate" value="@isoDate"onclick="javascript:SetDate('@isoDate');" />
                                                    @isoDate
                                                </div>
                                            }
Posted
Updated 14-Aug-15 2:16am
v2

1 solution

Hi, problem is that you have multiple elements with id="isoDate", that is not possible if you want to process it. Use class instead ;). This way you always only check first element! and that is reason why it work when first box is checked.

I've created simple pen here:
http://codepen.io/xszaboj/pen/RPOEGP?editors=101[^]

Of course you have to edit it to your needs, but it should provide the simple idea behind on click event and how to use it properly.

Do not use this: onclick="javascript:SetDate('@isoDate');" !!!

It will create event for every on of your checkbox in your case a lot. Try to catch it rather on parent element. in my demo #parent.
then in click event by using $(this) you will get element you clicked on, that way you will get value of element (date) and you can check if you have this date already in some global array.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Aug-15 11:17am    
Good catch. Using the same id makes an invalid HTML, the results are unpredictable. However, using the class attribute just for identification of set of element may be not the best idea, if this attribute is not used for styling. Look at jQuery: there are many kinds of selectors, including combinations of selectors, to select the set of elements. (I up-voted the answer with my 4.)
—SA
xszaboj 14-Aug-15 11:23am    
Yes, that is true. Class is not best solution, but it was first thing that come on my mind and is simple :). I choose different solution in my demo, you can check it ;).
Sergey Alexandrovich Kryukov 14-Aug-15 11:26am    
Sure. I just though it was important to mention.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900