Solved

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

Posted on 2013-12-05
3
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 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

Industry Leaders: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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