Get label for input field

前端 未结 10 552
野趣味
野趣味 2021-01-11 09:53

I\'m doing a validation with Jquery and need to get the $label from each element with their own label. Now the alert() gives med [object object]. The best thing for me here

10条回答
  •  被撕碎了的回忆
    2021-01-11 10:27

    Improving upon the answer you could wrap it in a function:

    function getLabel(name) {
        var $label = $("label[for='" + name + "']")
        return ($label.text());
    }
    
    var label = getLabel(this.id);
    console.log(label);
    

    This makes it much easier to reuse the code.

提交回复
热议问题