JQuery use variable by name of divID

Posted by Russell Parrott on Stack Overflow See other posts from Stack Overflow or by Russell Parrott
Published on 2010-12-28T07:49:43Z Indexed on 2010/12/28 7:53 UTC
Read the original article Hit count: 249

Filed under:
|

Just a quick question, that I cannot fathom out, hope you guys and girls can help.

I have a div (with ID) that when clicked opens a new div - great works well, what I really want is to "populate" the new div with predefined text based on the clicked div's ID. example:

<div class="infobox" id="help_msg1">Click me</div>

I may have say 3 (actually more but...) of these div's

This opens:

<div id="helpbox">text in here</div>

In/on my .js page I have doc ready etc then:

var help_msg1 ='text that is a help message'; var help_msg2 ='text that is another help message'; var help_msg3 ='text that is yet another help message';

Then

$('.infobox').live('click',function() {
 $('#helpbox').remove();
 $('label').css({'font-weight': '400'});
 $(this).next('label').css({'font-weight': '900'});
 var offset = $(this).next().next().offset();
 var offsetby = $(this).next().next().width();
 var leftitby = offset.left+offsetby+10;

$('body').append('text in here'); $('#helpbox').css( { 'left': leftitby, 'top': offset.top } ); });

Note I remove each #helpbox before appending the new one and the .next().next() identifies the appropriate text input that lot all works.

What I need is how do I put var help_msg1 into the append when id="help_msg1" is clicked or var help_msg2 when id="help_msg2" is clicked etc. I have tried

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about variables