Naveen Prakash
Naveen Prakash

Reputation: 41

how get dynamic element value by id using jquery

I am new to Jquery. And facing some issue while accessing dynamic element from my JSP using JQuery

  1. It works fine for me when I tried below code:

    function getListElement(index)
    {
        document.getElementById('dataList['+index+'].firstName').value;
    }
    
  2. Its not work when I tried below code:

    function getListElement(index)
    {
        $("#dataList["+index+"].firstName").val();
    }
    

Why 2nd point is not working may I know reason...... :(

Is there any other syntax to get that value....?

Thanks In Advance.....:)

Upvotes: 1

Views: 609

Answers (1)

James Montagne
James Montagne

Reputation: 78630

$("#dataList\\["+index+"\\]\\.firstName").val();

[] and . have special meaning in a selector. You might consider simplifying your ids.

Upvotes: 3

Related Questions