Solved

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

Posted on 2013-12-05
3
424 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Suggested Courses

624 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