JQuery use variable by name of divID

0 votes

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

Jul 19 in Web Development by gaurav
• 17,580 points
8 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Web Development

0 votes
1 answer

JQuery - Change event using element by name

How to trigger a event with event ...READ MORE

answered Jun 28 in Web Development by rajatha
• 7,040 points
24 views
0 votes
1 answer

Why use jQuery on() instead of click()

The . on() method attaches event handlers to ...READ MORE

answered Jun 27 in Web Development by rajatha
• 7,040 points
10 views
0 votes
0 answers
0 votes
1 answer

Why use jQuery on() instead of click()

on() differs from . click() in that it ...READ MORE

answered Aug 1 in Web Development by rajatha
• 7,040 points
12 views
0 votes
1 answer

What does cors means in Angularjs and what the use of it?

CORS is Cross Origin Resource Sharing which means you ...READ MORE

answered Jan 29, 2020 in Web Development by kartik
• 37,500 points
2,930 views
0 votes
1 answer

How to download a file by jQuery.Ajax?

Hello @kartik, You don't need to do this ...READ MORE

answered Sep 18, 2020 in Web Development by Niroj
• 82,740 points
5,613 views
0 votes
1 answer

Jquery - Using .each() with variables and an IF statement

To get values from elements, you need ...READ MORE

answered Jun 21 in JQuery by rajatha
• 7,040 points
17 views
0 votes
0 answers

jQuery + JSON how to define key from variable

I have the following code $.post( ...READ MORE

Jul 28 in Web Development by gaurav
• 17,580 points
11 views
0 votes
1 answer

How can we set environment variables from java?

For setting the environment variable, you can ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 960 points
2,716 views
0 votes
2 answers

difference between class and instance attributes

Apart from the performance, there is a ...READ MORE

answered Sep 17, 2018 in Python by SDeb
• 13,300 points
393 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP