How to get the data-id attribute?

后端 未结 15 2443
青春惊慌失措
青春惊慌失措 2020-11-21 23:29

I\'m using the jQuery quicksand plugin. I need to get the data-id of the clicked item and pass it to a webservice. How do I get the data-id attribute? I\'m using the .

相关标签:
15条回答
  • 2020-11-21 23:40

    If you are not concerned about old IE browsers, you can also use HTML5 dataset API

    HTML

    <div id="my-div" data-info="some info here" data-other-info="more info here">My Awesome Div</div>
    

    JS

    var myDiv = document.querySelector('#my-div');
    
    myDiv.dataset.info // "some info here"
    myDiv.dataset.otherInfo // "more info here"
    

    Demo: http://html5demos.com/dataset

    Full browser support list: http://caniuse.com/#feat=dataset

    0 讨论(0)
  • 2020-11-21 23:42

    Surprised no one mentioned:

    <select id="selectVehicle">
         <option value="1" data-year="2011">Mazda</option>
         <option value="2" data-year="2015">Honda</option>
         <option value="3" data-year="2008">Mercedes</option>
         <option value="4" data-year="2005">Toyota</option>
        </select>
    
    $("#selectVehicle").change(function () {
         alert($(this).find(':selected').data("year"));
    });
    

    Here is the working example: https://jsfiddle.net/ed5axgvk/1/

    0 讨论(0)
  • 2020-11-21 23:43

    To get the contents of the attribute data-id (like in <a data-id="123">link</a>) you have to use

    $(this).attr("data-id") // will return the string "123"
    

    or .data() (if you use newer jQuery >= 1.4.3)

    $(this).data("id") // will return the number 123
    

    and the part after data- must be lowercase, e.g. data-idNum will not work, but data-idnum will.

    0 讨论(0)
  • 2020-11-21 23:43

    Accessing data attribute with its own id is bit easy for me.

    $("#Id").data("attribute");

    function myFunction(){
      alert($("#button1").data("sample-id"));
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <button type="button" id="button1" data-sample-id="gotcha!" onclick="myFunction()"> Clickhere </button>

    0 讨论(0)
  • 2020-11-21 23:43

    This piece of code will return the value of the data attributes eg: data-id, data-time, data-name etc.., I have shown for the id

    <a href="#" id="click-demo" data-id="a1">Click</a>
    

    js:

    $(this).data("id");
    

    // get the value of the data-id -> a1

    $(this).data("id", "a2");
    

    // this will change the data-id -> a2

    $(this).data("id");
    

    // get the value of the data-id -> a2

    0 讨论(0)
  • 2020-11-21 23:43

    Try

    this.dataset.id
    

    $("#list li").on('click', function() {
      alert( this.dataset.id );
    });
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    
    <ul id="list" class="grid">
      <li data-id="id-40" class="win">
        <a id="ctl00_cphBody_ListView1_ctrl0_SelectButton" class="project" href="#">
          <img src="themes/clean/images/win.jpg" class="project-image" alt="get data-id >>CLICK ME<<" />
        </a>
      </li>
    </ul>

    0 讨论(0)
提交回复
热议问题