41421

Get product name from parent element

Question:

Here I am trying to get the product name in alert box when the "Buy now" button is clicked. Also I need to maintain the onclick event of the button for which I have placed the code in jquery.

<h2 class="product-name"><a href="product1.php" title="Sample Product Name">Sample Product Name</a></h2>

<a href="http://jsfiddle.net/LkKbz/" rel="nofollow">http://jsfiddle.net/LkKbz/</a>

The approach that I did is giving me blank alert box might be because I am calling parent element. Please help

Please note that here I don't want the alert when product name or image is clicked. Just want on button

Answer1:

Change this:

var text = $(this).parents(".item").find('.product-name').text();

Answer2:

here is answer

var text = $(this).parents('li').first().find('.product-name').text();

<a href="http://jsfiddle.net/LkKbz/1/" rel="nofollow">http://jsfiddle.net/LkKbz/1/</a>

Recommend

  • Which HTML5 tags are semantically correct to represent e-commerce products?
  • How to parse XML using XSLT?
  • Incomplete PHP class when serializing object in sessions
  • HOw to get CSV value for following scenario
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • Contact form problem - I do receive messages, but no contents (blank page)
  • How can I make this modal persistent?
  • Magento get URL before current
  • Reading a file into a multidimensional array
  • Simulate click Geckofx vb,net
  • Android application: how to use the camera and grab the image bytes?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Jquery UI tool tip close icon
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • MongoDB in PHP using aggregate to group by _id is null not working
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Display issues when we change from one jquery mobile page to another in firefox
  • HTML download movie download link
  • Upload files with Ajax and Jquery
  • To display the title for the current loaction in map in iphone
  • Do I've to free mysql result after storing it?
  • XCode can't find symbols for a specific iOS library/framework project
  • Cannot Parse HTML Data Using Android / JSOUP
  • A cron job substitute?
  • json Serialization in asp
  • trying to dynamically update Highchart column chart but series undefined
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal