質問

I have this JavaScript function:

function Test(isValid) {    
    var divStart = $get('divDateFrom');
    var divEnd = $get('divDateTo');
    var txtStartDate = divStart.firstChild;
    var txtEndDate = divEnd.firstChild;
    var isValidFromForecastStartDate;
    txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate.value);
}

This function is working fine in IE but I'm getting "txtEndDate.setattribute is not a function" error in Firefox and Chrome.

役に立ちましたか?

解決

Use jquery.attr() like,

$(txtEndDate).attr('dateInRegionalFormat', txtEndDate.value);

Updated there may be multiple elements so use [0] for the first element like,

txtEndDate[0].setAttribute('dateInRegionalFormat', txtEndDate.value);

You should first check whether the elements exists or not before setting attribute in it like,

if(txtEndDate.length)
{
   txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate.value);
}

他のヒント

You can do it by this way:

txtEndDate['dateInRegionalFormat'] = txtEndDate.value;

instead of old code:

txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate.value);

I have faced the same kind of issue very recently. Try to get rid of ".value" part. It may work for you.

txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top