Solved

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

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

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now