1

I know there are better ways to do this, but I'm currently trying to use Jquery to populate my pop-up email form's subject line to populate with the given class name of the current element. I've gotten it to the point where it will populate properly, but it is dumping in ALL of the names using that class, when i only want it to pull the specific one in which the button is nested:

var carname = $('.car-name').text();
$('input[id="email-subject"]').val(carname);

that is the Jquery of it, how can i get it so it only pulls that specific element's name vs. all of them?

thanks!

****EDIT: Additional HTML info:****

<div class="featuredcar">
 <img src="images/cars/1065_00.394180.jpg">
  <div class="featuredinfo">
   <h5 class="car-name">1968 Mustang</h5>
   <p>$10,000</p>
   <p><a href="#feat3" id="feat3">More info...</a></p>
   <p class="contact-p">
    <div class="contact-car">
     <i class="fas fa-envelope"></i>&nbsp;Contact Us</div> about this car!</p>
  </div>
</div>

 <div id="contact-car-form">
  <span id="close-form"><a href="javascript:void(0)" onclick="closeForm()">&times;</a></span>
   <h2>Want more Info?</h2>
   <small>I'll get back to you as quickly as possible</small>
   <form action="#">
   <input placeholder="Name" type="text" required />
   <input placeholder="Email" type="email" required />
   <input id="email-subject" placeholder="Subject" type="text" required />

I need the

input id="email-subject"

to be populated with the text value from

<h5 class="car-name">1968 Mustang</h5>

hopefully, this clears up any confusion. let me know if there is anything else I can provide and thank you for all of your help!

Andrew
  • 17
  • 4
  • 1
    Variations on this question are some of the most common jQuery questions here. It is perhaps hard to find them bcs everyone's use case is different, and the specifics vary. [The standard solution is the same though](https://stackoverflow.com/questions/14460421/get-the-contents-of-a-table-row-with-a-button-click), and is what @james-mcglone already answered - start with the element that was clicked and use that to identify the element you want. Eg, assuming you are clicking `fa-envelope`, using James' answer as template: `var carname=$(this).closest('.featuredcar').find('.car-name').text();` – Don't Panic Jan 04 '20 at 09:45
  • Yes, and thank you both @James McGlone and Don't Panic this would be a case of me not knowing all of my variables and options for JQuery, truthfully just getting my hands into it...and I tried numerous searches so i doubt i would've found this answer without your help! – Andrew Jan 05 '20 at 03:54

1 Answers1

1

This code will return the class name of the button's parent element. Which is 'test' in my example:

$(".btn").on('click', function(event){
  var carname = $(this).parent().attr('class');
  $('input[id="email-subject"]').val(carname);
});
<div class="test">
  <button class="btn">Click</button>
</div>
jpmc
  • 1,147
  • 7
  • 18
  • I understand what you're going for there, and understand how that works, but I can't use the class name, i need to have it use the specific class' text from only that element: `$('.car-name').text();` I need it to populate the field with ONLY that element's text/ If it helps, my `car-name` ID is part of a header element, i effectively want to call to the title element and have it populate in the subject field. – Andrew Jan 03 '20 at 08:54
  • I'm struggling to understand what you mean. Can you post more of your html please. – jpmc Jan 03 '20 at 10:03