Setting an attribute of a label element with an object-oriented method

Shortcuts have an “for” attribute, which causes them to point to a specific input field. I need to change the value of this attribute using jQuery so that I can use:

$("label").attr("for", "targetName");

But I also need to set className, so I would rather use:

$("label").attr({
    for: "targetName",
    className: "something" 
});

You may already have noticed the problem, because, unfortunately, the keyword is in javascript. Does anyone know how I can solve this? I am currently using the first method to set for and for the second to set several other attributes, it works, but it is not very pretty.

Any help would be greatly appreciated.

+3
source share
4 answers

htmlFor :

$("label").attr({
    htmlFor: "targetName",
    className: "something" 
});
+2

:

$("label").attr({
  'for': "targetName",
  'className': "something"
});

?

+5

Try something like:

$("label").attr("for", "targetName").attr("class", "something")

OR

$("label").attr("for", "targetName").addClass("something")

OR

$("label").attr({ "for": "targetName", className: "something" });
+2
source

That should work

$("label").attr("for","targetName").attr("class","something");
0
source

Source: https://habr.com/ru/post/1714396/


All Articles