...">
    Geek Answers Handbook

    Change HTML attribute using jQuery

    I am using jQuery 1.4.4. and I have this source code:

    <div id="area1"> <ul id="testlist" data-filter="false"> </ul> </div> 

    How to change data filter attribute to true in jQuery? I tried for example:

     $('#testlist').attr("data-filter").val(true); 

    but that will not work.

    Any idea?

    Best wishes.

    +4
    javascript jquery html
    Tim Dec 13 '10 at 15:48
    source share
    8 answers

    Since you are using jQuery 1.4.4 and "data" attributes, you can use the jQuery .data() method :

    Example: http://jsfiddle.net/patrick_dw/SLskn/

     alert( $('#testlist').data("filter") ); // alerts "false" $('#testlist').data("filter",true); alert( $('#testlist').data("filter") ); // alerts "true" 

    From the docs:

    Like jQuery 1.4.3 HTML 5, data attributes will be automatically pulled into the jQuery data object.

    +6
    user113716 Dec 13 '10 at 15:52
    source share

    The syntax of attr is $.attr(attributeName, value); . Try the following:

     $('#testlist').attr("data-filter", true); 
    +8
    Jonathon bolster Dec 13 '10 at 15:49
    source share

    You are using the wrong syntax. Try the following:

     $('#testlist').attr("data-filter", true); 
    +3
    James kovacs Dec 13 '10 at 15:49
    source share

    Try the following:

    $('#testlist').attr("data-filter", "true");

    Or maybe

    $('#testlist').attr("data-filter", true);

    +2
    Bojangles Dec 13 '10 at 15:49
    source share
     $('#testlist').attr("data-filter",true); 

    http://api.jquery.com/attr/

    +1
    wajiw Dec 13 '10 at 15:49
    source share

    Try $ ('# testlist'). attr ("data-filter", "true"); instead

    +1
    Dksan Dec 13 '10 at 15:50
    source share

    Proper Use of .attr - .attr(name, value)

    So change your code to

     $('#testlist').attr("data-filter", true); 

    http://api.jquery.com/attr/

    +1
    John giotta Dec 13 '10 at 15:50
    source share

    So you are using HTML5 syntax for custom attributes ... Nice.

    There is a jQuery metadata plugin that allows you to access these attributes in a more semantic way. Just use it like this:

     // Reading: var tooltipTitle = $(".tooltip").metadata().filter; // Writing: $(".tooltip").metadata().filter = "whatever"; 
    +1
    rsenna Dec 13 '10 at 15:55
    source share

    More articles:

    • Direct3D: efficient way to get system memory bitmap from IDirect3DSurface9 (default pool)? - directx
    • python on win32: how to get absolute time / number of processor cycles - python
    • How to use JSON in old browsers? - json
    • JSF custom converter for date - is it thread safe? - java
    • simpleXml behavior for arrays looks strange to me and - breaks at 9941 positions - arrays
    • jquery contents () returns node text for each character in IE - javascript
    • R: ape / phylobase: unable to convert ultrametric binary tree to hclust object (warning) - r
    • Vibrator in android - android
    • Prevent ItemContainerStyle from being installed already - c #
    • create binary search tree - java

    All Articles

    Geek Answers | 2019