SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $(".class secondclass").click();

    Hi Guys,

    Im trying to trigger a jQuery event on the click on one of my buttons. There are multiple buttons on the same page with the same class. Here's a snippet of the code. Any ideas why this is not working?

    HTML:
    Code:
    <input class="view-edit-button view-cv" type="submit" name="view1" title="1022" id="view1" value="View">
    jQuery:
    Code:
    $(".view-edit-button view-cv").click(function (e) {
    	e.preventDefault();
        var cv_id = $(".view-edit-button view-cv").attr("title");
    	// send cv id to script
    	// script will then start download
    	$.post('downloadcv.php', { id: cv_id }, 
    		function(data) 
    		{
    			if(data != 'error')
    			{
    				window.location.href = data;
    			}
    			else
    			{
    				$('#cv_result').html('<div class="errorbox">CV could not be downloaded.</div>');
    			}
    	});		
    });

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You haven't comma separated your classes, jQuery follows the same concept as CSS where all id's and classes MUST be comma separated or a parse error occurs. However in JavaScript the case is not the same and not error gets reported to the browser.

    Code JavaScript:
    $(".view-edit-button, .view-cv").click(function(e) {
        e.preventDefault();
     
        var cv_id = $(this).attr('title');
    });

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    You haven't comma separated your classes, jQuery follows the same concept as CSS where all id's and classes MUST be comma separated or a parse error occurs. However in JavaScript the case is not the same and not error gets reported to the browser.

    Code JavaScript:
    $(".view-edit-button, .view-cv").click(function(e) {
        e.preventDefault();
     
        var cv_id = $(this).attr('title');
    });
    Hi,

    Thanks for your help..The jQuery code is now getting triggered, however, its not getting the title attribute value. I think this is because I am using 5 other forms on the page and in the jQuery you listed above you used: var cv_id = $(this).attr('title');

    What would I need to change in order for this part to work?

    Thanks in advance.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The keyword this referrers to the current element object been the button you clicked on so there is no issue there, to confirm the code is sound i created a jsFiddle which works fine.

    http://jsfiddle.net/ybsZm/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •