3

Inside JavaScript, At runtime I am getting HTML code for checkbox inside a string as follows:

var chk="<input type='checkbox' id='checkbox1' name='chk1' />";

I want to get the ID of the checkbox.

My Question is:

Does jquery support any api to to retrive value of id?
Or
Do I have to follow the JavaScript api to achieve the same?

Note: Currently I am using following JavaScript code:

var i=chk.indexOf("id");
var s=chk.substring((i+4));
var j=s.indexOf("'");
s=s.substring(0,j);

Finally s contains id of checkbox.

2 Answers 2

7

Try this,

Live Demo

var chk = $("<input type='checkbox' id='checkbox1' name='chk1' />");

alert(chk.attr('id'));​
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you can do it easily with jquery, as follows:

var chk="<input type='checkbox' id='checkbox1' name='chk1' />";
id = $(chk).attr('id');
alert(id); //alerts 'checkbox1'

See working demo

1 Comment

@kayen Yes, that's another way to do it, thanks for pointing it out :-)

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.