Web Analytics Made Easy -
StatCounter Newbie looping and text replace question - CodingForum

Announcement

Collapse
No announcement yet.

Newbie looping and text replace question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie looping and text replace question

    I have a number of textbox elements on a page named with ids in the format:

    creUpload_inp_txt_multiDescription_0
    creUpload_inp_txt_multiDescription_1
    creUpload_inp_txt_multiDescription_2
    creUpload_inp_txt_multiDescription_3
    etc.

    I want to loop through them and replace their text (value)

    'John has a red car'

    to

    'John has a blue car'

    If someone could kindly educate me how to do this, it will get me off the runway to do everything else I require by myself. Just not sure how to reference elements without a precise name (elements have been pre named by code which produced the page - not my code) or how to tackle the text replace correctly.

    Any help much appreciated. Many thanks for reading.

  • #2
    Try this one...

    Code:
    for(var i = 0; i < total_textbox_count; i++) {
    document.getElementById('creUpload_inp_txt_multiDescription_'+i).value = 'John has a blue car';
    }
    Thanks & Regards,
    Niral Soni

    Comment


    • #3
      Many thanks for your reply Niral. That would work for my over simplistic example. However my real world useage is a little more complex. My apologies for over simplifying my question.

      Ok, so my page contains many text boxes, only some of which are identified as per my initial post. So what I really need to know is: How can I find text box with id = creUpload_inp_txt_multiDescription_xxx where xxx is the largest number of the text boxes named in this way please?

      Comment


      • #4
        Try this one...

        Code:
        var objs = document.getElementsByTagName('input');
        for(ele in objs) {
        if(ele.type.toLowerCase() == 'input' && ele.id.indexOf('creUpload_inp_txt_multiDescription_') == 0) {
        ele.value = 'John has a blue car';
        }
        }
        Thanks & Regards,
        Niral Soni

        Comment


        • #5
          Use a RegExp match()
          Code:
          var allInp=document.getElementsByTagName('input'), inp, i=0;
          while(inp=allInp[i++]){
          if(inp.id.match(/creUpload_inp_txt_multiDescription_/)){
          inp.value='new value';
          }
          }
          KOR
          Offshore programming
          -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎