Take the 2-minute tour ×

Country Name


Country Id

are two fields in my HTML page. I have used

<input id ="txtCountry" maxlength ="20" />

for my country text-box and nothing for my country id.

Now, I need to store and retrieve Country Id. To update the record I have a jquery and that needs my CountryId, how can I get that just using my input field.

$('txtCountry').attr('what comes here');
share|improve this question
I don't understand what your question is? –  Pekka 웃 Apr 15 '11 at 17:27
where is country name and country id on your page? –  Neal Apr 15 '11 at 17:28
I want to store countryid in my html form, I have only one control in my html form. How to do it ? –  nandu.com Apr 15 '11 at 17:28
@Neal : I have only country name in my page, I dont have country id. I am asking help how to add country id in my page(some type of control like thing to store country id in my html page). Can you get now ? –  nandu.com Apr 15 '11 at 17:34
It seems he wants to store arbitrary data in the HTML element. –  Rocket Hazmat Apr 15 '11 at 17:36
show 6 more comments

1 Answer

up vote 2 down vote accepted

It's unclear exactly what you're trying to do, but I suspect that you are trying to populate that input field on page load to make it possible for your user to update the value in the database.

I'm not sure what server techonology/framework you're using but to populate the textbox you would do something like this:

<input id ="txtCountry" maxlength ="20" value="CountryName" countryId="CountryId" />\

Then in jQuery you could access the country name like this:


And access the country id like this:

share|improve this answer
I suggest you use HTML5 data- attributes. data-countryId="CountryId", then you can do $("#txtCountry").data("countryId");. –  Rocket Hazmat Apr 15 '11 at 17:38
Thanks.Nick. This is what i am asking for. –  nandu.com Apr 15 '11 at 17:39
Thanks Rocket. I was struggling to implement the code you gave in old version of html. You helped me at right time. –  nandu.com Apr 15 '11 at 17:42
@Rocket, Is that cross browser? IE7/8+ etc? I haven't looked much into CSS3 or HTML5 as i'm assuming there are still a lot of users out there using older browsers. –  The Muffin Man Apr 15 '11 at 17:42
I'm not sure if IE7/8 can use HTML5. –  Rocket Hazmat Apr 15 '11 at 18:18
add comment

Your Answer


