Adding ID's to inputs - how?

Discussion in 'jQuery' started by Frederik G.L., Nov 18, 2013.

  1. #1
    Hi all.


    I've just started using jQuery so bear with me.



    We have an old interviewing system that spits out some auto-generated HTML code including these inputs (the only user-inputs are the texts (Text1-6)):



    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=10>Text1</span><br>

    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=11>Text2</span><br>

    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=12>Text3</span><br>

    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=13>Text4</span><br>

    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=14>Text5</span><br>

    <span class="mrSingleText">

    <input type=radio class="mrSingle" name=LIST9 value=15>Text6</span>


    As we would like to have our users being able to click the text next to the buttons, our idea was to use the <label for="x">Text x</label> function, but as this requires an ID on the inputs, we need to add ID's to the inputs; the input with "value=10" gets "id=10" and the input with "value=11" gets "id=11" and so on. And this needs to be executed on document.ready/body onload or something similar. Preferable this would be dynamic but we are not opposed to x seperate scripts as long as they are execute simultaneously.



    Any help would be greatly appreciated.
     
    Solved! View solution.
    Frederik G.L., Nov 18, 2013 IP
  2. #2
    
    <script type="text/javascript">
    $(document).ready(function() {
    
    $("input[type=radio]").each(function() {
      var getVal = $(this).val();
      //$("input").attr('value',getVal);
      $(this).attr('id',getVal);
      $(this).parent('span').prepend('<label for="'+getVal+'">'+'Text '+getVal+'</label>');
    });
    });
    </script>
    
    Code (markup):
     
    PoPSiCLe, Nov 18, 2013 IP
  3. Frederik G.L.

    Frederik G.L. Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    That's perfect. Works like a charm - you might just have saved our company ;) ^^ Thanks alot!
     
    Frederik G.L., Nov 18, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Would appreciate a "Best answer" if that's the case :)
     
    PoPSiCLe, Nov 18, 2013 IP
  5. Frederik G.L.

    Frederik G.L. Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Couldn't find the "best answer"-button the first time - sorry! Thanks a lot - i found it and used it!
     
    Frederik G.L., Nov 19, 2013 IP
  6. Frederik G.L.

    Frederik G.L. Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    One last thing:

    I figured out that we should append instead of prepend, but is there a way to append to the input tag instead of the parent span tag?
    $(this).parent('span').prepend('<label for="'+getVal+'">'+'Text '+getVal+'</label>');

    So the result is like this:
    <span><input type="radio" class="mrSingle" name="LIST10" value="11" id="11"><label for ="11">Text 11</label></span>
     
    Frederik G.L., Nov 20, 2013 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    This should work, if you want to have the label after the input:
    
    <script type="text/javascript">
    $(document).ready(function() {
       $("input[type=radio]").each(function() {
           var getVal = $(this).val();
           $(this).attr('id',getVal);
           $("#"+getVal+"").after('<label for="'+getVal+'">'+'Text '+getVal+'</label>');
         });
    });
    </script>
    
    Code (markup):
    Note that the text displaying will show up right after the label, so you might want to add some CSS to that, if you want it to move away a little bit.
     
    PoPSiCLe, Nov 20, 2013 IP
  8. Frederik G.L.

    Frederik G.L. Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Once again perfect. Thanks!
     
    Frederik G.L., Nov 20, 2013 IP