Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:44:21+00:00 2026-06-10T22:44:21+00:00

I have created an ajax link(CHTML::ajaxLink) in Yii framework(not ajax form submit button) that

  • 0

I have created an ajax link(CHTML::ajaxLink) in Yii framework(not ajax form submit button) that passes some value to a controller via ajax. There are multiple link which passes different value to the controller. I want to get the id/class attribute of the clicked link before the value is passed to the controller(in “beforeSend” of the jquery.ajax options ). Simply i just want to get the id/class attribute that generated the ajax request. Help!!!

UPDATE::Here’s the code

echo CHtml::ajaxLink ("Click Here",
                              Yii::app()->createUrl('default/del/id/6'), 
                              array(
                                    'beforeSend' => 'function(){
                        //I want to get the id of the link here     
    }',
                                    'complete' => 'function(){
                            }',

                                    'update' => '#loadContent'),

        );
The above code will generate the following a tag:-
<a href="#" id="yt1">Click Here</a>

When the user click the above link, i want to get the id (yt1) in the beforeSend part of the
ajaxLink.

I tried the below code:

 'beforeSend' => 'function(){
 $("a").click(function(){
    var a = $(this).attr("id");
    alert(a); 
 }

The above code works but the id is alerted only when the link is clicked twice. On the third click, the id is alerted twice and keeps on increasing on subsequent clicks. I dont have any clue about this strange problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:44:22+00:00Added an answer on June 10, 2026 at 10:44 pm

    You can use $.proxy(), to change the context of the function to anchor tag from the current ajax object:

    'beforeSend' => '$.proxy(function(jqXHR,settings){
            console.log($(this).attr("id"));// or alert($(this).attr("id"))
            // rest of your function
            // you can still use the jqXHR object, and the settings map to manipulate the ajax call
    },this)'
    

    Edit:

    Let me tell you why the number of alerts is growing on subsequent clicks.

    This is happening because each time you click, there is a new click handler getting associated to the <a>, because of this line:

    $("a").click(function(){...})
    

    So when you click the first time, the order of function calls is:

    beforeSend callback
    assign click handler (1)
    

    So there is no alert yet.

    Second time:

    1st click handler's alert
    beforeSend callback
    assign click handler (2)
    

    Third time:

    1st click handler's alert
    2nd click handler's alert
    beforeSend callback
    assign click handler (3)
    

    And so on as it keeps increasing.

    Edit2:

    Alternative and better, you can use context option to make the context, the link that was just clicked:

    'context'=>'js:this', // right now 'this' is link, so we can pass it
    'beforeSend'=>'function(){// i would suggest using the signature 'function(jqXHR,settings)' so that 
        // you can modify the ajax call if you need to
    
        console.log($(this).attr("id"));// or alert($(this).attr("id"))
        // rest of your function
    }'
    

    From jquery’s ajax documentation:

    By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax).

    Edit3:

    Another alternative: Pass the id of link as an additional setting key:

    'idOfLink'=>'js:$(this).attr("id")',
    'beforeSend'=>'function(){
         // now you can access the id like:
         console.log(this.idOfLink);// or alert(this.idOfLink)
    }'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom widget which has a link to some ajax functionality.
I have a div that is created by Ajax with .append(html) function, I'm trying
I have just created add input field with <a id=add>Add</a> link button which is
I have a simple form created using Ajax.BeginForm : <% using (Ajax.BeginForm(Update, Description, new
I have created jquery-ajax call for php page that creates content by selecting data
I have a form with two textboxes and a submit button. The second box
I have created ajax code for my city tabs to display respective city data
i have a unordered list of links, which are dynamically created by Ajax, and
I have this AJAX loader, created using CSS. And I m trying to increase
I have a very AJAX-ish listing webpage created using Codeigniter where I want registered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.