is it possible to set custom attribute by jQuery attr() function using JS variables

rain picture rain · Sep 21, 2011 · Viewed 45.6k times · Source

I'm using jquery 1.5 and html4 standard.
I'm trying to set custom attribute which i get by javascript variable, but it is not setting up.code sample:

var attname="list1"; // this is changed on every call of the function where it is defined.    
var attvalue="b,c,d"; // this also changed.  
jQuery('#div1').attr({attname:attvalue});

but it treat attname as string itself rather variable. there are other option like using data(),prop() but they are supported in HTML5 and jquery 1.6 that is not possible for me at the moment.other problem is data can't be set on server side to be sync and used at client side by jquery data(). as they are syntactically diff. if there's some other way please suggest Thanks.

Answer

Sedat Başar picture Sedat Başar · Sep 21, 2011

I guess you should use this

jQuery('#div').attr(attname,attvalue);