Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
435 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

722 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