ConorBaumgart
ConorBaumgart

Reputation: 513

Trying to use an id from the same page that my .click function was triggered on, in an ajax function

newbie to coldfusion/jquery/programming general here. So the overview of my problem is this: I have a ticket id that corresponds with a specific row in my database. When I click a button, I would like one of the columns in that row to change values to "In Testing". My issue is that I do not know how to pull that ticket id number into my jquery function, or if this is even possible. My code:

<script src="/TicketFaster/js/jquery-1.11.3.js"></script>
<script src="/TicketFaster/js/scripts.js"></script>

<cfset ticketid="#ticketid#">

<button id="in_testing" type="button">In Testing</button>

my js:

$(document).ready(function() {
$("#in_testing").click(function() {
    var x = (#ticketid#);
    $.ajax({
        url: 'ticketcomponent.cfc?method=in_testing',
        type: 'POST',
        data: {
            test: x
        }
    });
});
});

The big problem is that these pages are being generated dynamically, so each one will have a different ticket id. Therefore, I need to have the ticket id variable be imported rather than just hard coded in to the jquery function. So is this possible? I did not include the query because it works fine when I use it in other places, just getting the data delivered is the tough part. I appreciate any help you can give me :)

Edit: I was requested to post what I'm trying right now. The original coldfusion is the same so I'm not going to post that again. Here is the js I'm using:

$(document).ready(function() {
    $("#in_testing").click(function() {
        var x = (<cfoutput>#ticketid#</cfoutput>);
        alert(x);
    });
});

(I also tried without the cfoutput tags) As you can see, I'm just trying to do a simple alert to check if my variable has been correctly set. Once I get that to work, then the ajax should follow fairly quickly because I have some experience in that.

Upvotes: 0

Views: 101

Answers (1)

Alfonso Jaquez
Alfonso Jaquez

Reputation: 103

What is your specific issue?, can you share a jsfiddle?

for dynamic events replace

$("#in_testing").click(function() {});

for

 $(document).on('click','#in_testing', function() {});

This value

 var x = (#ticketid#);

in jquery is some like that

 var x = $('#ticketid').val(); // for value or $('#ticketid') for object 

you just have to take account id created dynamically

Upvotes: 1

Related Questions