JQuery uses a variable called divID

Just a quick question that I can’t understand, I hope that you guys and girls will help you.

I have div(with an identifier) ​​that opens a new one when clicked div- it works fine, I really want to "fill" the new one with divpredefined text based on the clicked div identifier, Example:

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

I can say 3 (actually more ......) of these divs

This will open:

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

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

var help_msg1 = 'text, which is a help message'; var help_msg2 = 'text, which is another help message'; var help_msg3 = 'text, which is 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 (' ');  $ ('# helpbox'). css ({'left': leftitby, 'top': offset.top});  });

. #helpbox , .next().next() , .

, var help_msg1 append id="help_msg1" var help_msg2 id="help_msg2" ..

+3
3

<div>, jQuery data():

$(document).ready(function() {
    $("#help_msg1").data("help_msg", "text that is a help message");
    $("#help_msg2").data("help_msg", "text that is another help message");
    // etc...
});

<div>:

$(".infobox").live("click", function() {
    // ...
    $("body").append($(this).data("help_msg"));
});
+3

.

var messages = new Array;
messages['help_msg1'] = 'text that is a help message';

....

, , , :

var id = $(this)[0].id;
if(typeof(messages[id]) != 'undefined'){
     $('body').append(messages[id]);
}else{
     alert('no message for id '+id);
}
+1

-

$('.infobox').live('click',function(event) {
    var text = eval(event.target.id);
    ...
}
0

Source: https://habr.com/ru/post/1782412/


All Articles