/    Sign up×
Community /Pin to ProfileBookmark

Reference Value of Input Element Inside $(this) Object

Totally blanking on how to construct – multiple HTML forms on page, ex:

<form action=”” method=”post” role=”form” id=”someid”>
<input type=”hidden” name=”id” value=”9″>
<input type=”submit” value=”Submit”>
</form>

using function below to submit via AJAX (works fine).

$(function() {
$(‘form’).on(‘submit’, function (e) {
$.ajax({
type: ‘post’,
url: ‘my.php’,
data: $(this).serialize(),
success: function () {
}
});
e.preventDefault();
});
});

How do I reference an individual data item inside the “on submit” function to get it’s value, like:

$(this).input[name=id].val() [does not work]

I want the value of the input name id, IE: “9”

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 25.2017 — IMO there is no need to use "this" for this purpose. Just use:[CODE]$("input[name='id']").val();[/CODE]or if you like to use "this":[CODE]$(this).find("input[name='id']").val();[/CODE]
Copy linkTweet thisAlerts:
@cruntauthorMay 25.2017 — I wish these were the answer, as they were the first things I tried - these will return the first instance of:

<input type="hidden" name="id" value="some_number">

on the page, instead of the form that is actually being submitted - as noted in OP, there are multiple forms on the page.

$(this).serialize() is obviously pulling the data from the correct place, as it has the value of the selected name=id in it. How can I mimic that for a single value?

Thanks for your reply.
Copy linkTweet thisAlerts:
@SempervivumMay 25.2017 — I understand. If so, doesn't my second snippet fix the issue? If not, please post more code: From where do you access this input element? My code will not work, if you access it inside the success callback.
Copy linkTweet thisAlerts:
@cruntauthorMay 25.2017 — Doh, I apologize Sempervivum, the second example does work, I was console.log outputting in the wrong place in my test page - thanks for your help and consider this closed!
Copy linkTweet thisAlerts:
@rootMay 26.2017 — Also note that JQuery questions go in their own forum called JavaScript Frameworks and when posting code, please use the forums BB Code tags.
×

Success!

Help @crunt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...