Jump to content

Issue getting value of textarea when displayed in Facebox


Solarpitch

Recommended Posts

Hi,

 

I'm using the JQuery lightbox "Facebox" to display a textarea. I display the text area with a button. When the user types text into the textarea and clicks submit it wont pass the value to the Jquery function, but works fine when the textarea is not displayed in the lightbox.

 

JQuery Function

$(function() {
$(".insert").live('click', function() {

	var desc = $("freetext").val();
	alert(desc);


	return false;
});
});

 

 

<a href="#freetext" rel="facebox">Add Free Text</a> // popup lightbox with textarea and button


   <div id="free_text" style="display:none;">
  
<TEXTAREA NAME="freetext" id="freetext" class="freetext" COLS=40 ROWS=6>Test</TEXTAREA><br/>
<INPUT TYPE=SUBMIT VALUE="Insert"  class="insert" id="insert">

  </div>

 

So basically, when I click insert in the lightbox it returns the value as "Test" which has been hard coded in the textarea. However when I ad anything to this it still returns "Test" so is not picking up the dynamic input at all.

Link to comment
Share on other sites

Any example page?

 

I plugged your HTML code and JQuery into a page and everything works, after changing "display:none" to "display:block" and using $("#freetext").val(). Note the #freetext. It is probably something else on the page that is causing the unexpected behaviour if using the ID selector still does not solve the issue.

Link to comment
Share on other sites

It seems to be that the jQuery only reads from the value of the textfield when it is not display using Facebox. When we use Facebox to popup the textfield and click insert, it still reads from the textfield on the page so to speak, like, kinda ignores the value in the lightbox.

 

I dont have an example page at the moment but can probably set one up later if you have no other suggestions.

Link to comment
Share on other sites

Problem is facebox is not moving the content you display in a facebox but its making a copy of it.  So therefore you have 2 instance of the textarea when it displays.  If you change the click handler to the following it works.

$(function() {
$(".insert").live('click', function() {

	var desc = $(this).parent().find('textarea').val();
	alert(desc);


	return false;
});
});

Link to comment
Share on other sites

I've never used facebox, but I don't think it's right to have 2 elements with the same ID. Defeats the entire purpose of IDs IMO, and can cause these kinda unexpected stuff to happen. IDs were never meant to be duplicated!

Link to comment
Share on other sites

Problem is facebox is not moving the content you display in a facebox but its making a copy of it.  So therefore you have 2 instance of the textarea when it displays.  If you change the click handler to the following it works.

$(function() {
$(".insert").live('click', function() {

	var desc = $(this).parent().find('textarea').val();
	alert(desc);


	return false;
});
});

 

This sorted the problem. And Sean your right, doesnt make sense to have two ID's the same but Facebox basically copied the one that's set to display:none; and shows it in the lightbox (Facebox). Thanks for your help guys.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.