1

I have a problem, I need to obtain the values for html elements by name using Jquery ussually I would do it like this $("input[name=form_size]).val(); but what if my name attr value looks like this name="data[referralMedium][referral_id]" how do I write the JQuery code?

0

4 Answers 4

4

You need to put quote marks around it:

$("input[name='data[referralMedium][referral_id]']").val();

Note that technically the quote marks are always required. See the docs for the attribute equals [name="value"] selector.

Sign up to request clarification or add additional context in comments.

4 Comments

In addition, you can create your own selector like #id, .class etc. Say for example you want $("--some_name") to return the elements with the name "some_name". This is possible to achieve with a custom selector.
So this will then also work if($("input[name='data[ReferralMedium][referral_id]'] option:selected").val() == 13){
@Roland How can an input have a descendant option?
@lonesomeday - My Bad, it should be select not input
1

The name has to be quoted:

var element = $('input[name="data[referralMedium][referral_id]"]');

Comments

1

That's a good question. What I might do is class each of those inputs and get the value by class rather than by name. I think it's faster, too.

  <input type="text" name="data[referralMedium][referral_id]" class="referralMedium_referral_id"/>

  $('.referralMedium_referral_id').val();

Comments

0
$('input[name="data[referralMedium][referral_id]"]').val();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.