2

I need to dynamically alter a variable with the "name" attribute from a link - code below...

       <input type="text" class="datepicker" id="dp1">

       <a href="javascript:;" class="tab" name="1,2">test button</a>

and

$(document).ready(function(){

    var pickable = { dp1: [4,5,6] };

    $(".tab").click(function () { 
        var test = $(this).attr("name").split(",");
        pickable = { dp1:  test  };
    });

    $(".datepicker").each(function() {
        $(this).datepicker({
            beforeShowDay: function(date){ 
                var day = date.getDay(), days = pickable[this.id];
                return [$.inArray(day, days) > -1, ""];
            },
        });
    });

});​

Any ideas why this doesn't work??

5
  • what do you get? the string "1, 2" instead? Commented Sep 29, 2010 at 8:43
  • not sure to be honest... how do i find out? Commented Sep 29, 2010 at 8:51
  • The var "test" value is always a string Commented Sep 29, 2010 at 8:53
  • and i need it to be an array?? is that right? Commented Sep 29, 2010 at 8:55
  • @joberror: Actually, from the code above, test is an array containing the single string "1, 2" Commented Sep 29, 2010 at 9:12

3 Answers 3

1

You can try something like this

$(document).ready(function(){

var pickable = ["2","3","4","5"];

function closedDays(date){
    var sDate = date.getDay().toString();
    if ($.inArray(sDate, pickable) == -1) return [false,"",""];
    else return [true, ""];
}

$(".tab").click(function () { 
   pickable =  $(this).attr("name").split(",");
     closedDays;
});

$(".datepicker").each(function() {
    $(this).datepicker({
        beforeShowDay:  closedDays
    });
});
});​

You can try it here http://jsfiddle.net/ZKW3b/2/

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

Comments

1

If you mean that the function passed to click is not being executed, check whether that code is being interpreted before the html.

Try wrapping the code in the ready function:

$(function () {
    var pickable = { dp1: [4, 5, 6] };
    $(".tab").click(function () { 
        var test = [ $(this).attr("name") ];
        pickable = { dp1:  test  };
    });
});

If the problem is that you want the string value of name, "1, 2", to be an array, you need to change your code a bit:

var pickable = { dp1: [4, 5, 6] };

$(".tab").click(function () { 
    var test = $(this).attr("name").split(",");
    pickable = { dp1:  test  };
});

Now, the value "1, 2" is being split by the , token and seperated into an array of values: ["1", "2"]

2 Comments

I have already done that, just didn't include it in the code i posted. sorry
I see what you're saying but it still doesn't work... here's a link to the code put in context, perhaps that'll work... jsfiddle.net/BhzE5/2
0

If you intend to have var pickable available anywhere in your code, you might want to use, jquery data. You can try something like this:

    $(document).data('pickable', { dp1: [4, 5, 6] } );
    $(".tab").click(function () { 
        var test = $(this).attr("name").split(',');
        $(document).data('pickable', { dp1:  test  });
    });

1 Comment

Your example will create an array within an array: [["1", "2"]]

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.