0

Below is my code,I want to get values of p_id by using it's name. As i am new to javascript,please help me.

   <form>

        <input name="p_id[]" value="0"/>
        <input name="p_id[]" value="1"/>
        <input name="p_id[]" value="2"/>

    </form>
2
  • too broad. can you elaborate?? Commented Jan 26, 2015 at 6:40
  • document.getElementsByName... ? Commented Jan 26, 2015 at 6:40

3 Answers 3

1

You can use pure javascript method:

document.getElementsByName('p_id[]');

To get values of a particular one :

document.getElementsByName('p_id[]')[0].value;
Sign up to request clarification or add additional context in comments.

2 Comments

Here is my code, I just want to get the values of selected checkboxes by using it's name which is an array. <form> <input type="checkbox" name="p_id[]" value="0"/> <input type="checkbox" name="p_id[]" value="1"/> <input type="checkbox" name="p_id[]" value="2"/> </form>
please help with this also.!
0

You may want to try like this:

document.getElementsByName("p_id[]")[0].value;

Try this fiddle:

http://jsfiddle.net/qhmohco5/

<form>

    <input name="p_id_1[]" value="0"/>
    <input name="p_id_2[]" value="1"/>
    <input name="p_id_3[]" value="2"/>

</form>

Comments

0

Since you tagged JQuery:

$('input[name*="p_id"]').each(function(){
    console.log($(this).attr('value'))
})

Fiddle

This function finds all the input tags with the name containing the phrase p_id and logs their values.

The *= operator performs substring matching, so you can specify a pattern that you want to match.

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.