views:

223

answers:

3

I am creating form fields dynamically.


If I use

<script>
$().ready(function() {

    $("input[name=salesPrice1]").blur(function() {

        var nameID = $("input[name=salesPrice1]").val();

        alert(nameID);

        //var newName = $("input#newName").val();

        $.ajax({
            type: "POST",
            url: "cashPrice.cfm",
            data: "salePrice=" + $("input[name=salesPrice1]").val(),
            cache: false,
            success: function(data) {
                $("#cashPrice1").html(data);
            }
        });
    });
});
</script>

It will partially work. Now, I have to get the ID/Name of the formField dynamically. How do I do this?

+1  A: 

Your question is vague at best. But is this somewhat along the lines of what you want?:

$("input").blur(function ()
{
    var that = $(this);

    var id = that.attr("id");
    var name = that.attr("name");
});



Update:

You can match elements on values:

$("input[id^='hello']")

Will match:

<input type="text" id="helloworld" />
<input type="text" id="helloearth" />

But not:

<input type="text" id="hiworld" />

See the selector documentation.

NOTICE: these selectors are slow and I would only use 'em as a last resort. To speed up performance, you can do queries on a subset of DOM nodes:

$("complex selector goes here", $("container node in which to query"))
roosteronacid
I am having trouble with the code formatting on this site. I have the following <script>$().ready(function() {alert(1); $("input").blur(function () { var that = $(this); var id = that.attr("id"); var name = that.attr("name"); });});</script><body><form><cfoutput><cfloop from="1" to="3" index="i"><input type="text" name="salesPrice#i#" id="salesPrice#i#" value="" /><div id="cashPrice#i#"></div><hr /></cfloop></cfoutput></form>I don't think i can simply use $("input") since this function needs to be applied to only salesPrice.
FALCONSEYE
Also, I can see the alert(1) when the page loads,but nothing else.
FALCONSEYE
+2  A: 

Try this ?

$("input[name^=salesPrice]").each(function(){
    var input = $(this); 
    var name = input.attr('name');
    var num = /\d+$/.exec(name)[0];

    $(this).blur(function() {

        var nameID = input.val();

        alert(nameID);

        //var newName = $("input#newName").val();

        $.ajax({
        type: "POST",
        url: "cashPrice.cfm",
        data: "salePrice=" + nameID,
        cache: false,
        success: function(data) {
            $("#cashPrice"+num).html(data);
        }
        });
    });
});
subtenante
almost. I had to change : data: "salePrice=" + input.val(),
FALCONSEYE
Wups... Ok, let's use nameID which has already been computed before then. Edited the answer. :)
subtenante
A: 

this also works: ` $().ready(function() { alert('loaded'); $('.salesPriceInput').blur(function () {
alert($(this).attr("id")); var myID = $(this).attr("id").substr(10,1); alert(myID); $.ajax({ type: "get", url: "cashPrice.cfm", data: "salePrice=" + $('#salesPrice'+myID).val(), cache: false, success: function(data){ $('#cashPrice'+myID).html(data); } }) }); });


FALCONSEYE
The text editor keeps ignoring my <form> I meant to add <form><cfoutput><cfloop from="1" to="3" index="i"><input type="text" name="salesPrice#i#" id="salesPrice#i#" class="salesPriceInput" value="" /><div id="cashPrice#i#"></div><hr /></cfloop></cfoutput></form>
FALCONSEYE
You should skip lines and indent the code with at least 4 spaces for the formatter to format your code. Be careful with substr : some day you may change the names in your coldfusion file and forget to update your javascript code... then you'll scratch your head pretty hard.
subtenante