Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select an input element based on ID and set its “checked” attribute to “true” using Jquery

Posted on 2013-12-05
3
Medium Priority
?
437 Views
Last Modified: 2013-12-05
Hi experts!

Using Jquery, I am trying to select an input based on ID and set its “checked” attribute to “true”.

But it’s not working.

Here is my code:
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
 
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js" 
            type="text/javascript"></script>
 
        <script>
 
            $(document).ready(
                     function () {
                         $('input[id="1"]').checked = true;
                     }
             );
        </script>
 
    </head>
    <body>
       <input  type="Radio" name ="radio" id="1"     />
    </body>
</html>

Open in new window


I tried these other versions below but they all don’t work:
$('input["#1"]').checked = true;
$('#1').checked = true;

Please advise.

Thank you! Much appreciated!
0
Comment
Question by:nvs_victor
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39698905
Please note "1" is not a valid ID, it should, MUST, start by a letter.

Test page : http://jsfiddle.net/cUe7r/1/
But the following work :
            $(document).ready(
                     function () {
                         $('#1').attr("checked", true);
                     }
             );

Open in new window

0
 

Author Comment

by:nvs_victor
ID: 39699365
Please note "1" is not a valid ID, it should, MUST, start by a letter.
Thank you for the tip! However, the Razor view engine makes it cumbersome to fix it. But that's another story.

Thank you! Problem solved thanks to you! :)
0
 

Author Closing Comment

by:nvs_victor
ID: 39699369
Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question