1

Ok so what I am trying to do is use javascript to get the attribute that I select, and use that as the javascript variable. Better if I show you anyways.

var a='<div id="element">Hello</div>';
var b="<span class="class">GoodBye</span>";
var c="<div id="element3">Filler Text</div>";
var d="<div id="element4">Filler text again</div>";
var e="<div id="element5">Filler text once more</div>";

$('.element').click(function(){
  var Element = $(this).attr('data-edit');
  $('#information_inner').html(Element);
  $('.pop_up').show();
 });

HTML markup would look like

<div class="element" data-edit="c"></div>

So we already know what is going to happen, the HTML will become c not the variable listed above. So how would you go about to retrieving the data-edit and changing it or making it to select as the javascript variable it list?

EasyBB
  • 6,176
  • 9
  • 47
  • 77

1 Answers1

2

You want to probably move those variables into an object:

var text = {
    a: '<div id="element">Hello</div>',
    b: '<span class="class">GoodBye</span>',
    c: '<div id="element3">Filler Text</div>',
    d: '<div id="element4">Filler text again</div>',
    e: '<div id="element5">Filler text once more</div>'
}

Then you can use, e.g., text['a'] to access one of these:

$('#information_inner').html( text[Element] );
pauljz
  • 10,803
  • 4
  • 28
  • 33
  • Oh silly me :) I see I see. Thank you very much, as I never thought of this. I was wondering how in the world to do this. I appreciate this as all other answer on here are so simple and my question was more complex than those I've read.!!! Upvote and check! – EasyBB May 18 '13 at 02:15
  • I just used this and set two objects, one named title other named body. And it works awesomely! Thank you SO much I now understand the objects just a little bit more! you are awesome for helping! – EasyBB May 19 '13 at 13:35