1

Got the following JS code.

<script language="javascript">
fields = 0;
pNR = 0;
err=0;
function addInput() {
    if (fields != 40) {
        var firstInput = document.createElement("input");
        var secondInput = document.createElement("input");

        firstInput.type = secondInput.type = "text";
        firstInput.name = "firstfield" + pNR;
        secondInput.name = "secondfield" + pNR;


        var text = document.getElementById("text");
        text.appendChild(firstInput);
        text.appendChild(secondInput);
        text.appendChild(document.createElement("br"));
        fields += 1;
        pNR += 1;
    } else ...

</script>

I want to set a class for each tag created. The class should be the name of the field and "required":class="firstfield0 required"`

1 Answer 1

2

You can use Element.className, it gets and sets the value of the class attribute of the specified element.

firstInput.className = "firstfield0 required";
Sign up to request clarification or add additional context in comments.

1 Comment

Uhh, I was using firstInput.class . Problem solved. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.