SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find the radio button with same value as variable, then set it checked

    Using javascript, I can accomplish this with the following:
    Code:
    for (var cnt=0; cnt<4; cnt++){                            
     if (document.form1.txtsty[cnt].value == txtsty){         
      document.getElementById('txtsty').selectedIndex = cnt;  
      document.form1.txtsty[cnt].checked = 'checked';         
      break;                                                  
     }                                                        
    }
    How can I accomplish this same thing using jquery?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The difficulty is in applying the same index value of the radio to the select box, so pretty much the same code structure.

    Assuming that the form is identified with a unique identifier, you could use something like this:

    Code javascript:
    $('[name="txtsty"]', '#form1').each(function (value, index) {
        if (value === txtsty) {
            $('#txtsty').prop('selectedIndex', cnt);
            this.prop('checked', 'checked');
            return false;
        }
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul. Seems like its a bit easier to just leave the javascript as I have it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •