javascript - How can I get data from multiple attributes? - Stack Overflow

I want to get data from multiple attributes on click event. Suppose I have a div with class test_class

I want to get data from multiple attributes on click event. Suppose I have a div with class test_class and ID testID as given below.

<div class="test_class" id="testId"><!-- ... --></div>

I want to get the value of the class and id attributes and store them in separate variables.

var classattr = 'test_class';
var idattr = 'testId';

How can I make this? I would like to avoid calling attr() multiple times.

I want to get data from multiple attributes on click event. Suppose I have a div with class test_class and ID testID as given below.

<div class="test_class" id="testId"><!-- ... --></div>

I want to get the value of the class and id attributes and store them in separate variables.

var classattr = 'test_class';
var idattr = 'testId';

How can I make this? I would like to avoid calling attr() multiple times.

Share Improve this question edited Jun 9, 2017 at 10:14 Just a student 11.1k2 gold badges44 silver badges71 bronze badges asked Jun 9, 2017 at 9:48 user7791702user7791702 2904 silver badges18 bronze badges 4
  • use .attr() like $("#testId").attr("id") for id and $("#testId").attr("class") for class – guradio Commented Jun 9, 2017 at 9:50
  • @guradio Is there is any way to get the values in once I mean I don't have to use attr() multiple times – user7791702 Commented Jun 9, 2017 at 9:52
  • you are still going to use .attr() map them in an array using map and getting .attr() – guradio Commented Jun 9, 2017 at 9:53
  • Store it in a variable. – Sagar V Commented Jun 9, 2017 at 9:54
Add a ment  | 

10 Answers 10

Reset to default 2

You can get those with javascript too, like below.

var attrs = document.getElementById("testId").attributes;
var classattr = attrs["class"].nodeValue;
var idattr = attrs["id"].nodeValue;
console.log(classattr);
console.log(idattr);
<div class="test_class" id="testId">

</div>

Even simpler with object destruction:

var t = document.getElementById("testId");

var {className, id} = t;

console.log(className, id)
<div class="test_class" id="testId">

You can add any other attributes between surly braces if you need.

console.log("Div id is "+$('div').attr('id'))
console.log("Div class is "+$('div').attr('class'))
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test_class" id="testId"></div>

  1. Use .attr()

    Description: Get the value of an attribute for the first element in the set of matched elements.

USE:

$(this).each(function() {
  $.each(this.attributes, function() {
    // this.attributes is not a plain object, but an array
    // of attribute nodes, which contain both the name and value
    if(this.specified) {
      console.log(this.name, this.value);
    }
  });
});

You will not have to use .attr multiple times.
For eg: a single span tag with multiple attributes can be obtained using the above code.

what you can do is when a certain button has been pressed. lets say the class btn has been clicked.

<html>

    <a href="#" class="button">Click me</a>
    <div class="test_class" id="testId> Some stuff in here </div>

</html>

<script>

    $("").click(function(){

var dvclass = $(".test_class").attr("class");
var dvid = $(".test_class").attr("id");


});


</script>

You don't need jQuery at all. By the magic of vanilla JavaScript, you can already access the class and id of any element you have. There is even a neater interfaces for adding/removing classes: classList. You can use them as follows, in a jQuery click handler:

$('.test_class').click(function() {
  console.log(this.className, this.id);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test_class" id="testId">Click me!</div>

Or without any jQuery:

document.querySelector('.test_class').addEventListener('click', function() {
  console.log(this.className, this.id);
});
<div class="test_class" id="testId">Click me!</div>


To access other attributes, you can use other methods.

  • data-* attributes? Use dataset.
  • name attribute? Well... name.
  • Attribute that cannot be accessed directly? getAttribute() has you covered.
function getDomNodeProps(domNode) {
    let attrs = {}
    Array.from(domNode.attributes).forEach(a => attrs[a.name] = a.value);
    return attrs;
}
console.log(getDomNodeProps(document.getElementById("testId")))

Try with attr().And do with click function .Then find the attr value using this.attr

$('.test_class').click(function() {
  var cls = $(this).attr('class');
  var id = $(this).attr('id');
  console.log(cls, id)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test_class" id="testId">click</div>



    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("div").click(function(){
            var clnm=$(this).attr("class");
            var idSel=$(this).attr("id");
        });
    });
    </script>
    </head>
    <body>

    <div class="test_class" id="testId">
    Example
    </div>
    </body>
    </html>
$("div").click(function(){ var classattr=$(this).attr("class"); var idattr=$(this).attr("id"); console.log(classattr,idattr); });
$(this).each(function() {
  $.each(this.attributes, function() {
    // this.attributes returns array
    if(this.specified) {
      console.log(this.name, this.value);
    }
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744221476a4563801.html

相关推荐

  • javascript - How can I get data from multiple attributes? - Stack Overflow

    I want to get data from multiple attributes on click event. Suppose I have a div with class test_class

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信