Reputation: 11
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
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
Reputation: 407
Try this ($() has been added to index)
alert($(code).find('li').index($('.my-class')));
Update: Fiddle sample
Upvotes: 0
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
Reputation: 418
try this
$('#fc-send2').live('click',function () {
var code = $('textarea.site-code');
alert($(code).find('li').index('.my-class')); });
Upvotes: 0
Reputation: 6996
Instead of
var code = $('textarea.site-code').val();
Use
var code = $('textarea.site-code');
Upvotes: 2