1

I'm trying to pass a dynamically created id from a form to JavaScript.

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    var fld = "<?= $div_id;?>";
    var test = $(fld).val();
        alert(test);
...

<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value="" maxlength="80" />
<input type="submit" value="Send" class="button-call" name="p_k2" id="p_k2" />
</form>

alert(test) shows: undefined.

when I change it to alert(fld) I get two alerts, first it shows an empty box, then the correct $div_id

what am I doing wrong??

EDIT 1: I'm trying to display the text from the input...

EDIT 2: this is the complete JavaScript:

<script type="text/javascript">
$(function() {
  $(".button-call").click(function() {
    //var fld = "<?= $div_id;?>";
    var fld = "input[name=<?=$div_id;?>]";
    //var fld = "#<?=$div_id;?>";
    var test = $(fld).val();
    var dataString = 'content='+ test;

    alert(fld);
    alert(test);

    if(test=='')
    {
        alert("Write some text ...");
    }
    else
    {
        $("#flash").show();
        $("#flash").fadeIn(1000).html('<img src="images/ajax-loader.gif" align="absmiddle"> <span class="loading"> Loading ...</span>');

      $.ajax({
      type: "POST",
      url: "p_k2.php",
      data: dataString,
      cache: false,
      success: function(html){
      $("#divToRefresh").after(html);
      document.getElementById(fld).value='';
      document.getElementById(fld).focus();
      $("#flash").fadeOut(1000);
      }
      });
    }
    return false;
  });
});
</script>

EDIT 3: this is the example I'm using: http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html everything works OK if I use fixed id. But can't get it working with dynamically created id.

EDIT 4: OK, there are many answers, but non of them seem to be working. I'll try to simplify.

I'll get back to this example, the one I started with (http://www.9lessons.info/2009/05/insert-and-load-record-using-jquery-and.html).

My website is something similar to Facebook. You have friends, and on your 'wall' page you have many different comment boxes (for each of your friends). To make ID's of those comment boxes different from one another (to avoid posting something to all your friends) I had to use friend id's from database (input type="text" class="tb" name="<?php echo $div_id; ?>" id="<?php echo $div_id; ?>" value=""). That part works OK.

I can get that id to the JavaScript (var fld = "input[name=<?=$div_id;?>]";) but, I can't read the value of that text field. Once I do that, I'll take care of inserting that to my database...

1
  • look at my answer, it should work Commented Jul 24, 2011 at 12:10

3 Answers 3

1

try

var fld = "input[name=<?=$div_id;?>]";

or better

var fld = "#<?=$div_id;?>";
Sign up to request clarification or add additional context in comments.

13 Comments

first option shows two alerts, again. first one: input[name=], and the second: input[name=correct_div_id]... second shows nothing, and firebug shows: uncaught exception: Syntax error, unrecognized expression: #
What does $div_id contain? alert($("input[name='some_name']").val()); should work
It contains id from mysql database. I have comment boxes, similar to facebook. There are lots of comment boxes on one page, that's why I had to put a dynamically created id's. It does show the correct value from the textbox on the second alert!
can you post a sample script and html outputted by your application on jsfiddle.net
There is still no problem, see ideone.com/6dGOd (there is dynamic code, which replaces $div_id with your example). When you copy and paste the result output to htmledit.squarefree.com then all is fine. If this still solves not your problem, then there may be an other problem in your source, which has nothing to do with this question
|
1
var test = $("#"+fld).val();
alert(test);

11 Comments

sure, but as you can see, he wants to get content from input field into variable test
what's different from my solution?
have a look at the example from edit3. I can't get it working with variable id of <input type="text" ...
@rabudde: I do not look at everyone's answer, so I'm not sure if you have exactly the same. I just edited it
@Alex: look at your fld = '?' and tell us what ? contains
|
1

you can use $(this) inside the click event handler.

i.e.

$(".elementClass").click(function(){
    alert($(this).attr("id"));
});

will return the id of the .elementClass that was clicked.

1 Comment

this helps a bit if change the button id to my variable id... $(function() { $(".button-call").click(function() { var fld = $(this).attr("id"); var test = $("#"+fld).val(); alert(test);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.