Reputation: 75
I have a few tags, here is the code
<p class="post-meta-ab"><span><a href="#">Diamond Burs set of 30 pcs A-24</a></span>
<br /><span><i class="icon-circle"></i>Details: Diamond Burs set of 30 pcs very useful assorted shapes</span>
<br /> <span><i class="icon-circle"></i>Shipping Weight: 100 gms</span>
<br /><span><i class="icon-circle"></i>Price: € 2.50</span>
<br /> <span><i class="icon-circle"></i>Quantity: <input type="text" style="width:20px" maxlength="100" name="a1" id="a1" />
<input type="submit" value="Add" style="margin-top:-8px" class="btn btn-danger" name="asubmit" id="a1sub" /></span>
</p>
My question is I am using
var productname = $(this).parent().html();
to get the details, but the details are coming of the tag, I want to get the tag of the span's tag i.e "p" tag's html.
EDIT X: I want to get the data inside the input type="text" is it possible?
EDIT X2: I am using this now
$(this).parent().parent().text()+$(this).parents('p').find('input:text').val()
it tends to work, ok last thing I want to do is, Put space after each details, example put space after shipping weight, put space after price and so on.
Upvotes: 2
Views: 10902
Reputation: 839
To know who the parent is or isn't you would have needed to tell us what is $(this). Since you didn't here goes the code to get the value of your edit field.
alert($(".post-meta-ab").find("#a1").attr("value"));
http://fiddle.jshell.net/hYhT6/
EDIT Now we know who &(this) is... :-) I would go with the sieblings and not parent as you intially. In this case if you use parent you will have to then do some traversing so why not do it directly from the current location.
One more edit A solution has been accepted but nevertheless here is my last take at it. The prev() method would come handier; like this:
$("#a1sub").click(function() {
alert($(this).prev().attr("value"));
})
And the updated working fiddle
Upvotes: 1
Reputation: 128796
To get the parent's parent you can simply call the parent()
method on the parent:
$(this).parent().parent();
Alternatively you can use jQuery's closest()
method:
$(this).closest('p');
Here's a JSFiddle demo which shows both in use when initially targeting i.icon-circle
.
To get the text, you can then simply call jQuery's text()
method:
$(this).closest('p').text();
As per an edit to the question, to pull the value of the text input you can either use:
$(this).closest('p').find('input[type="text"]').val();
Or, as the input has an id
of "a1" (which is always unique), you can just use:
$('#a1').val();
As per a further edit to the question, to put a space between the result you'd simply add it in using ... + " " + ...
, like so:
...closest('p').text() + " " + $(this).closest('p')...
Upvotes: 4
Reputation: 4624
Try this
$(this).closest('p').find('input:text').val();
or
$(this).parents('p').find('input:text').val();
Upvotes: 1
Reputation: 388436
You can use .closest() along with .outerHTML if you want the html including <p>...</p>
$(this).closest('p')[0].outerHTML
Upvotes: 1