0

html :-

<div id="456"></div>

JQuery :-

var myvar = "456";

$('?----myvar-----?').addClass('newclass');

is it Possible to access "id" without writing $('#123'). i want to use id by using variable

2
  • Here is simple script $('#'+myvar).addClass('newclass'); Commented Dec 3, 2019 at 6:40
  • FYI, when adding tags, only add the tags that are relevant to the question/issue. This is a pure js/jquery question so I removed the PHP tag. Commented Dec 3, 2019 at 6:51

1 Answer 1

2

Here is a simple script:

var myvar = '456';
$('#'+myvar).addClass('newclass');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="456">Test Div</div>

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

Comments

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.