Ferg
Ferg

Reputation: 111

Appending a variable to divs when I click on the div itself

Can't seem to get this to work have tried using the this in jquery but it doesn't work any idea would be great. I am aiming at the variable changing on a click and then appending on next click of the <'div'>. These should be independent and I have the code for switching the variable.

Cheers Ferg

HTML

<div id="en">test</div>
<div id="de">testing</div>
<input type="button" value="Click Me"></input>

Javascript

var lang='de';

$('#en').click(function (){
  lang='en';
});

$('#de').click(function (){
  lang='de';
});

$(function(){
  var NewContent='<div class="added">Added</div>'
  $(this).click(function(){
      $("#spin").after(NewContent);
  });
});

jsFiddle is here

Cheers Again

Update

I have updated the fiddle here to be clearer jsfiddle i want to be able to specify which div the variable goes into.

Upvotes: 0

Views: 2766

Answers (2)

Tyron
Tyron

Reputation: 1976

I suppose this is what you want (JSFiddle at http://jsfiddle.net/vgATZ/):

HTML

<a href="#en">english</a>
<br>
<a href="#de">german</a>
<br><br>
<div class="gogo">Test div 1</div>
<div class="gogo">Test div 2</div>

JS

var lang='de';

$('a[href="#en"]').click(function () {
    lang='en';
    return false;
});

$('a[href="#de"]').click(function () {
    lang='de';
    return false;
});

$("div.gogo").click(function(){
  var newContent='<div class="added">Added '+lang+'</div>';
    $(this).append(newContent);
});

Upvotes: 1

Juice10
Juice10

Reputation: 240

I am guessing adding something like this would work for you:

$("div").click(function(){
  var newContent='<div class="added">Added '+lang+'</div>';
    $('body').append(newContent);
});

Upvotes: 0

Related Questions