Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Is it possible to use a regex in Javascript's "getElementById" method?

  • 12-02-2009 1:46pm
    #1
    Registered Users, Registered Users 2 Posts: 3,906 ✭✭✭


    Hi All,

    Apologies if this is a stupid question, I write all my code in Ruby but at the moment need to use some Greasemonkey scripts for a specific test and my Javascript is very, very limited... I've googled around, but can't find an exact answer... I essentially have a script that fills in various text fields, selects items from lists, etc on specific pages and that can be done easily with something like this:

    document.getElementById("name").value = "1";

    The problem is, the controls I am working with have names that vary to a certain degree on each execution or different pages. In my Ruby code, I can use regular expressions when identifying a control to work with but I don't see a way to do this with JS? I'd be looking to do something along these lines:

    document.getElementById(/[\w\d]+/).value = "1";

    Can something like that be done? How?

    Thanks in advance.


Comments

  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    try this,

    it loops through all the elements in FornName then checks if their names match the regex expression (not tested by the way)
    var re = new RegExp(/[\w\d]+/);
    
    for(i=0; i<document.FormName.elements.length; i++)
    {
      var m = re.exec(document.FormName.elements[i].name);
      
      if (m != null) {
        //element name matches regex, so do something with element;
      }
    }
    


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Or we could try doing what he asked for stephen :)
    var re = new RegExp(/[\w\d]+/);
    var m = re.exec(document.getElementById($whatever_id).value);
    if (m != null) {
        //element name matches regex, so do something with element;
    }
    

    RD


  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    my way is better than your way !!

    im a bit of a rookie with javascript !!


  • Registered Users, Registered Users 2 Posts: 3,906 ✭✭✭J-blk


    Thanks, but none of these work for me... I'm sure I'm doing something wrong though. To make things clearer, let's take http://www.msn.com which has a text field with an id of "f1". So I could adapt your example Ron to be something like:

    var re = new RegExp(/f1/);
    var m = re.exec(document.getElementById($whatever_id).value);
    if (m != null) {
    alert(m);
    }

    Nothing is returned?


  • Registered Users, Registered Users 2 Posts: 1,045 ✭✭✭Bluefrog


    Seems to be some confusion here between names and ids. You're looking to match to an id right? So...
    var theForm = document.forms[0];
    // declare our regex
    var re = new RegExp(/[\w\d]+/);
    // loop through the form elements
    for(i = 0; i < theForm.elements.length; i++){
    // execute our regular expression against each element id
    var m = re.exec(theForm.elements[i].id);
    if (m != null) {
        //element id matches regex, so do something with element id cause that's what we want to match
    alert('We have a match on the form control with an id of ' + theForm.elements[i].id);
    }
    }
    


    Ooooooooh, this is post 666 for me, the post of the beast!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,906 ✭✭✭J-blk


    Thanks Bluefrog - but this doesn't seem to work for me either. I quickly tested it on www.msn.com, replacing the regex with f1, which is the id of the search text field on the page. Nothing is returned once again...


  • Registered Users, Registered Users 2 Posts: 1,045 ✭✭✭Bluefrog


    Well, ran it myself on the msn page and debugged. Two issues - looking back at the documentation I think we should be using the test method rather than exec. Also, the f1 element you're looking for is in the second form on the page - document.forms[1]. Script below works - you could always amend it to loop through all the forms.

    Should have double-checked this before I posted last time. Just stick the code below before the closing body tag.
    <script type="text/javascript">
    function checkRegs(){
    var theForm = document.forms[1];
    // declare our regex
    var re = new RegExp(/f1/);
    // loop through the form elements
    for(i = 0; i < theForm.elements.length; i++){
    // execute our regular expression against each element id
    var m = re.test(theForm.elements[i].id);
    if (m) {
        //element id matches regex, so do something with element id cause that's what we want to match
    alert('We have a match on the form control with an id of ' + theForm.elements[i].id);
    }
    }
    }
    
    document.onLoad = checkReg();
    </script>
    


  • Registered Users, Registered Users 2 Posts: 3,906 ✭✭✭J-blk


    Thanks a lot Bluefrog, that does indeed work like I originally wanted :D. So I should be able to adapt it for the pages I'm working with. Thanks again!


Advertisement