Using jQuery Variable in .html()

Hello Experts!

I have a series of images that the user can click on. When the user clicks on one of the images, I want to replace the contents of div#Video with a jquery variable. I tried using .html("$" + this.id), but this isn't working. Do I need to use a different function? Thank you!

<div id="Video">Video Title Goes Here</div>

<img id="Agency" alt="Agency with Doug" src="myImage.png" />

$(document).ready(function() { 
		
  $Agency = '<h4>You are now viewing Agency with Doug Video.</h4>';

  $("img").click(function() {    
    $("#Video").html("$" + this.id);
  });
});

Open in new window

spogglesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
$("#Video").html($(this).attr("id"));

Open in new window

0
spogglesAuthor Commented:
div#Video is only showing the word Agency and not the html stored in the variable $Agency.
0
Sudhindra A NCommented:
try

$("#Video").html(jQuery.globalEval("$" + $(this).attr('id')));
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

spogglesAuthor Commented:
This did not work either. With the other code that was posted, it would switch the contents to "Agency". The below code does switch anything at all.

$("#Video").html(jQuery.globalEval("$" + $(this).attr('id')));
0
amit_gCommented:
$("#Video").html($Agency);

Open in new window

0
spogglesAuthor Commented:
I could do this - $("#Video").html($Agency); - but I wanted to use the id of the image that was clicked. I have several images on screen and would like to avoid coding a big if/then/else statement.
0
amit_gCommented:
You should explain what you want and that way it is much simpler to answer. I think you need something like this (based on the markup and code you have posted)...

$(document).ready(function() { 
		
  $("img").click(function() {    
    $Agency = '<h4>You are now viewing ' + $(this).attr("alt") + " Video.</h4>';
    $("#Video").html($Agency);
  });
});

Open in new window

0
ProculopsisCommented:
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 
  <style type='text/css'>
    .title { display: none; }
  </style>

<script type='text/javascript'>//<![CDATA[
$(function(){
$("img").click(function() {
    $("#Video").replaceWith($(".title").show());
});
});//]]>  

</script>

</head>
<body>
  <div id="Video">Video Title Goes Here</div>

<img id="Agency" alt="Agency with Doug" src="myImage.png" />

<h4 class="title">You are now viewing Agency with Doug Video.</h4>

</body>
</html>
0
Michel PlungjanIT ExpertCommented:
.......html(window["$"+this.id]);

$Agency is NOT a jQuery var but just a window scoped var
0
amit_gCommented:
There is no need to keep the $Agency outside of click handler. All the OP seems to need is the id or Alt attribute of clicked image and put it in a string.  http:#a37743386 should work.
0
Michel PlungjanIT ExpertCommented:
@amit of course not. There is a string per image.
An even better idea is a JS object array like
var texts = {
'Agency':'<.......>',
'Otherimage':'<......>',
'more'':'<.......>'
} // notice no comma at the end

And then use
$("#Video").html(texts[this.id]);
0
amit_gCommented:
@mplungjan, agreed.
0
Michel PlungjanIT ExpertCommented:
Also add a class so not ALL images on the page gets a click

So code should look like

<div id="Video">Video Title Goes Here</div>
<img id="Agency" class="vidimg" alt="Agency with Doug" src="myImage.png" />
<img id="Video1" class="vidimg" alt="Video #1 with Doug" src="myImage.png" />
<img id="Video2" class="vidimg" alt="Video #2 with Doug" src="myImage.png" />

<script>
var texts = {
'Agency':'<h4>You are now viewing Agency with Doug Video.</h4>',
'Video1':'<h4>You are now viewing Video 1 with Doug Video.</h4>',
'Video2':'<h4>You are now viewing Video 2 with Doug Video.</h4>'
}

$(document).ready(function() { 
  $(".vidimg").click(function() {    
    $("#Video").html(texts[this.id]);
  });
});
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.