0

Is there anyway to get the name of a class when it is unknown to jquery

<input type="checkbox" id="if you tick me" class="tickme" onClick="myfunction(this)">
<input type="checkbox" id="you tick me also" class="tickme">

function myfunction(x) {
                $('."unknown class name goes here"').prop('checked', 'true')
}

therefore when the first one is checked the second one is two but you dont know either of the class names but the class names are the same and you have to use a class and not and id.

2
  • 2
    You have to use a selector. So maybe $(':checkbox')? But just out of curiosity, why don't you know the classname? Commented Feb 18, 2014 at 14:38
  • Just use any of the many ways jQuery allows you to select the element. You're not limited to only classes and IDs. Commented Feb 18, 2014 at 14:38

1 Answer 1

1

You could do something like this

function handleClick(x) {
    $("." + $(x).attr("class")).prop('checked', true)
}

this will get the class name from the this keyword

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

2 Comments

perfect thats just what i needed thank you so so much.
What happens if the element has more than one class?

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.