user1800796
user1800796

Reputation: 11

Get the index of the element

I have a code into the textarea. Here it is http://clip2net.com/s/2tnj2 When i click to submit button, all this code put into the variable.

$('#fc-send2').live('click',function () {

var code = $('textarea.site-code').val();

alert($(code).find('li').index('.my-class')); });  

But it allways show me "-1". That mean, element not found. How to find index of the element from variable "code".

Upvotes: 0

Views: 102

Answers (5)

fearmear
fearmear

Reputation: 539

$('#fc-send2').live('click',function () {

  var code = $('textarea.site-code').val();

// code var is a String, not a DOM element

  alert($(code).find('li').index('.my-class'));

// that's why you can't do .find here

}); 

You should

var someHiddenContainer = $("#some-hidden-container");

someHiddenContainer.html( code ); //insert your code var to DOM

alert( someHiddenContainer.find("li").index(".my-class") );

someHiddenContainer.empty(); //optional

Upvotes: 0

AlexT
AlexT

Reputation: 407

Try this ($() has been added to index)

alert($(code).find('li').index($('.my-class')));

Update: Fiddle sample

Upvotes: 0

Arun Manjhi
Arun Manjhi

Reputation: 173

Just replace the line

var code = $('textarea.site-code');

with

var code = $('#textarea.site-code');

you have just missed the # with the Id.

Upvotes: 0

Younus Ali
Younus Ali

Reputation: 418

try this 

$('#fc-send2').live('click',function () {

var code = $('textarea.site-code');

alert($(code).find('li').index('.my-class')); });  

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 6996

Instead of

var code = $('textarea.site-code').val();

Use

var code = $('textarea.site-code');

Upvotes: 2

Related Questions