malina malina - 27 days ago 7
jQuery Question

Javascript remember variable, strip div

I have the a script which adds a word to a div on mouse-over now I want the word to be remembered and printed in the console when clicking on the word.
But the words are links like:

<a href"">So,</a>


When I click on the word, the script now remembers the whole thing with the link. So I need to find a way how to "strip" it down to just the word "So" any ideas?
Here the code:

<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

<script>
var $allescheissehierrein = $('.allescheissehierrein'),
datenbank = {
1: '<a href="file:///Users/shirin/Desktop/A/Media%20Design/TCB/one.html">So,</a>',
2: '<a href="file:///Users/shirin/Desktop/A/Media%20Design/TCB/one.html">HERE,</a>'},
simbol = '',
platzierRandom = function () {
var w = document.body.offsetWidth,
h = document.body.offsetHeight,
rd = document.getElementsByTagName('div');

for (var c = 0, l = rd.length; c < l; c++) {
if (rd[c].className !== 'random') {
continue;
}

var xCoord = Math.floor(Math.random() * w),
yCoord = Math.floor(Math.random() * h);

switch (true) {
case (xCoord >= w - rd[c].offsetWidth - 10):
xCoord = w - rd[c].offsetWidth - 10;
break;
case (xCoord <= 10):
xCoord = 10;
break;
case (yCoord >= h - rd[c].offsetHeight - 10):
yCoord = h - rd[c].offsetHeight - 10;
break;
case (yCoord <= 10):
yCoord = 10;
break;
}

rd[c].style.left = xCoord + 'px';
rd[c].style.top = yCoord + 'px';
}
};

//einfach loop mit bis 100 oder was auch immer
for (var i = 1; i <= 2; i++) {

//einen div in eine variable speichern
//es wird für die datenbank "data-hover" auf den i wert gesetzt welcher ja stetig hochzählt darum alle verschieden
switch (true) {
case (i < 24):
simbol = '#';
break;
case ((i > 23) && (i < 47)):
simbol = '**';
break;
case ((i > 46) && (i < 97)):
simbol = '-';
break;
case ((i > 96) && (i < 114)):
simbol = '1.';
break;
case (i > 113):
simbol = '~~';
break;
}

//diesen die dem allescheissehierrein-div appendieren (100 mal)
$allescheissehierrein.append('<div data-hover="' + i + '" class="random"><p>' + simbol + '</p></div>');
}

//diese platzier dings funktionion erst nach der platzierung aller divs (diese 100) machen weil sie ja vorher gar nicht existieren
platzierRandom();

//richtig mit jquery UND datenbank
$('.random')
.hover(function () {
var hoverdata = $(this).data('hover'),
satzvondatenbank = datenbank[hoverdata];
$(this).append($('<span>' + satzvondatenbank + '</span>').show('slow'));


$( 'a' ).mousedown( function( event )
{
var poemArray;
if ( localStorage.getItem( "poem" ) )
{
poemArray = localStorage.getItem( "poem" );

console.log( poemArray );
}
else
{
poemArray = "";
localStorage.setItem( "poem", poemArray );
}

poemArray += " " + satzvondatenbank;

console.log( poemArray );


localStorage.setItem( "poem", poemArray );

}
)



}, function () {
$(this).find('span:last').remove();
});











</script>

</html>

Answer

You can navigate to the anchor tag using jquery and then get the text using jquery like this,

var anchorText=$("a").text();

This will fetch the innerText which is present inside the anchor tag. Or you could use this method as well,

var anchor=<a href="file:///Users/shirin/Desktop/A/Media%20Design/TCB/one.html">So,</a>

anchor.substring(indexOf(">")[0],indexOf("<")[1]);