1

I have access to a form that I need to modify a labels text. I can only add JS to the page otherwise I'd just change the HTML or even replace the text with CSS.

To make matters worse the label doesn't have an ID or NAME other than it's "for" attribute.

<label for='camper1Grade' class="control-label required">Grade entering, Fall 2020</label>

If I could just edit the CSS I would do this:

   <style>
    label[for=camper1Grade]:before {content: "Grade completing, Spring 2020";}
    label[for=camper1Grade] { font-size: 0px;}
    label[for=camper1Grade]:after { content: "*";color: red;}
    </style>

But like I mentioned I can't add CSS directly.

I'm a pretty good CSS coder but know very little about JS. I'm sure there is a solution I just can't find a good explanation of how to do this. Thanks in advance to anyone who has an idea.

1 Answer 1

4

Almost all elements you can select with CSS you can also select with Javascript and querySelector. Unfortunately, ::before and ::after psuedo-elements are not among them. But, you can always inject another stylesheet using Javascript:

document.body.appendChild(document.createElement('style')).textContent = `
    label[for=camper1Grade]::before { content: "Grade completing, Spring 2020"; }
    label[for=camper1Grade] { font-size: 0px; }
    label[for=camper1Grade]::after { content: "*"; color: red;}
`;

(note the two colons, which is the standard for CSS3)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that totally worked. Once I figured out how to get the function to run - I'm really really bad with JS and Jquery - I really need to study up on it more.

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.