jquery javascript concatenate/variable issue -


i have html table inside of form tags. when click on cell inside of table jquery gives me “id” attr of cell.
save “id” attr variable called id. when alert “id” id shows:

    alert(id); //outputs 39 

however on next line have

      term1 = $form.find( "input[name='firstoneinput_" + id +"']").val();   alert(term1); //outputs undefinined // should output input                                         value name attribute                                        of "firstoneinput_39" 

why alert outputting undefined. btw put real values in text fields also. html

                <!doctype html>                          <html lang="en">                          <head>                             <meta charset="utf-8">                             <title>jquery.post demo</title>                             <script src="http://code.jquery.com/jquery-1.9.1.js"></script>                          </head>                          <body>                             <form action="comments.php" id="nameform">                                <table border='1'>                               <tr>                <td><input type="text" class="hidden" name="firstoneinput_1_25" id="25" placeholder="firstoneinput_1_25"> </td>     <td>  <input type="text" class="hidden" name="firsttwoinput_1_26" id="26" placeholder="firsttwoinput_1_26"></td>  </tr> <tr>     <td>  <input type="text" class="hidden" name="firstoneinput_2_27" id="27" placeholder="firstoneinput_2_27"> </td>     <td>  <input type="text" class="hidden" name="firsttwoinput_2_28" id="28" placeholder="firsttwoinput_2_28"></td>  </tr> <tr>     <td>  <input type="text" class="hidden" name="firstoneinput_3_29" id="29" placeholder="firstoneinput_3_29"> </td>     <td>  <input type="text" class="hidden" name="firsttwoinput_3_30" id="30" placeholder="firsttwoinput_3_30"></td>  </tr>                <tr>     <td>  <input type="text" class="hidden" name="firstoneinput_4_31" id="31" placeholder="firstoneinput_4_31"> </td>     <td>  <input type="text" class="hidden" name="firsttwoinput_4_32" id="32" placeholder="firsttwoinput_4_32"></td>  </tr>                </table>            <td> <input type="submit" value="search"></td>              </form>            <!-- result of search rendered inside div -->                 <div id="result"></div> 

javascript

               <script>                  $(document).ready(function(){                  var id = 0;                 $('table tr td').click(function(){                 id = $(this).children("input[class='hidden']").attr("id");                  return id;                      });               $( "#nameform" ).submit(function( event ) {             // stop form submitting               event.preventdefault();         // values elements on page:                var $form = $( );                 term1 = $form.find( "input[name='firstoneinput_" + id +"']").val();                   alert(term1); <---alerts undefined thou put in form values                    alert(id); <----alerts ok id 

new answer

this alert undefined ...

alert(term1); // < ---alerts undefined thou put in form values 

... because you're looking firstoneinput_xx ...

$form.find( "input[name='firstoneinput_" + id +"']").val(); 

... actual input elements have name convention firstoneinput_x_xx.

<input ... name="firsttwoinput_1_26" ... /> 

- - - previous answer - - 8-< - - -

html:

<form>   <table>     <tr>         <td id='39'>klick</td>     </tr>   </table>       <input name="firstoneinput_39" value="the_uber_value"/> </form> 

script:

$(document).ready(function() {   $('td').click(function() {     alert($('input[name="firstoneinput_' + this.id + '"]').val());   }); }); 

magic: http://jsfiddle.net/mcnull/4bdmh/


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -