'How to access element through variable ID in Jquery
I am writing a code where I want to check a radio button through Jquery, there are some links, when I click on a specific link it checks a radio button , Id of radio button is passed to jquery click function, I get that id and store it in variable , how to access that radio button element through var id!!!!
//Html
<ul class="footer-links">
<li>Services</li>
<li><a href="#scrollup-content" data-id="tab-1">Brand Management</a></li>
<li><a href="#scrollup-content" data-id="tab-2">In & Outdoor Signage</a></label></li>
</ul>
//jquery
$(".footer-links li a").on("click",function(){
var id = $(this).data();
console.log(id)
$('#tab-2').attr('checked', true); // this is working properly
$('#id').attr('checked', true);// how to do? I want this to work
})
Solution 1:[1]
I think there is a problem with the elements in the HTML part: I can't see that you have added a radio button to the file. Check out the sample code below:
$('#radioButtonForm input').on('change', function() {
alert($('input[name=radioName]:checked', '#radioButtonForm').val());
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<title>Test Project</title>
</head>
<body>
<form id="radioButtonForm">
<ul>
<li>Services</li>
<li>
<input type="radio" name="radioName" value="1" />Brand Management<br />
</li>
<li>
<input type="radio" name="radioName" value="2" />In&Outdoor Signage<br />
</li>
</ul>
</form>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sercan |
