0

I have a jQuery function like below.

function resultfucntion(state) {
        if (!state.id) {
          return state.text;
        }
        
        var state_output = $("<span data-tooltip='"+state.value +"'>" +  state.text +
            "<span>(" + state.text1 + ")</span></span>"
        );

        return state_output;
      }

I would like to pass HTML code as content value to below CSS code

span:hover:before {
    content: attr(data-tooltip);
    position: absolute;
    padding: 5px 10px;
    margin: -3px 0 0 180px;
    background: orange;
    color: white;
    border-radius: 3px;
}

I am getting output like below

enter image description here

I read this post.

Now I am looking for a JavaScript or jQuery way to pass HTML value as CSS content.

abu abu
  • 6,599
  • 19
  • 74
  • 131
  • 1
    It's not clear to me what you're trying to do here, but you certainly don't want to use that CSS. Span elements are very common, and you don't want every one of them having those styles on hover. – isherwood Mar 03 '22 at 02:07
  • Thanks @isherwood. I'll change span element later. Actually I would like to show Tooltip using CSS content attribute. Thanks. – abu abu Mar 03 '22 at 02:09
  • 1
    Could you explain a little more what you want when you say “pass HTML code as content”. It looks as though that is what you have done. Did you expect the content to interpret the HTML rather than just treat it as a string? – A Haworth Mar 03 '22 at 02:25
  • Thanks @AHaworth. Did you expect the content to interpret the HTML rather than just treat it as a string? – Yes. `state.value` has HTML content. I would like to show as Tooltip. Thanks. – abu abu Mar 03 '22 at 02:31
  • 1
    You will I think need to put the HTML in the DOM not as content in a pseudo element. – A Haworth Mar 03 '22 at 03:26
  • Thanks @AHaworth. Could you please help me to solve the issue ? Thanks. – abu abu Mar 03 '22 at 03:30
  • 1
    Does @rezasaadati answer not help? – A Haworth Mar 03 '22 at 03:35
  • Thanks @AHaworth. No, I am getting output like this https://i.stack.imgur.com/Lgy4W.png. Could you please help me in this post ? https://stackoverflow.com/questions/71320946/tooltip-is-not-appearing-properly – abu abu Mar 03 '22 at 06:46
  • 1
    I'd suggest that you get this question fully sorted before asking aother, similar, one. The undefined must be possible for you to track down - what are you writing to that item? We don't have enough of your code and context to be able to work that out. – A Haworth Mar 03 '22 at 09:13
  • Thanks @AHaworth. Actually I am trying to use a Tooltip on Select2. I am trying in two ways. Any one solution is helpful for me. I was trying to fix `undefined`. But I couldn't solve the issue. Thanks. – abu abu Mar 03 '22 at 09:31
  • Thanks @AHaworth. Here is my Code. https://jsfiddle.net/abufoysal/qjudp87g/ – abu abu Mar 03 '22 at 09:43

1 Answers1

1

I'm not quite sure if it's possible to do it the way you want it. But this may be a solution how you could achieve the same goal:

$('span').hover(function() {
  $(this).after(`<div class="tooltip-box">${$(this).attr('data-tooltip')}</div>`);
  $('.tooltip-box').show();
}, function() {
  $('.tooltip-box').hide();
});
.tooltip-box {
  position: absolute;
  padding: 5px 10px;
  margin: -3px 0 0 180px;
  background: orange;
  color: white;
  border-radius: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span data-tooltip="I am <strong>strong</strong>, <em>emphasized</em> and <mark>marked</mark>.">Hyperlink</span>

Update

It is not clear to me what the point of your function should be. However, this is how you could combine the above code with your function:

function resultfucntion(state) {
  if (!state.id) {
    return state.text;
  }

  var state_output = `<span data-tooltip='${state.value}'>${state.text}<span>(${state.text1})</span></span>`;

  return state_output;
}

const state = {
  id: 1,
  value: 'I am <strong>strong</strong>, <em>emphasized</em> and <mark>marked</mark>.',
  text: 'Hyperlink',
  text1: 1
}

$('body').append(resultfucntion(state));


$('span').hover(function() {
  $(this).after(`<div class="tooltip-box">${$(this).attr('data-tooltip')}</div>`);
  $('.tooltip-box').show();
}, function() {
  $('.tooltip-box').hide();
});
.tooltip-box {
  position: absolute;
  padding: 5px 10px;
  margin: -3px 0 0 180px;
  background: orange;
  color: white;
  border-radius: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Reza Saadati
  • 5,018
  • 4
  • 27
  • 64
  • Thanks @Reza Sasdati. Could you please do something inside function `function resultfucntion(state) {}` ? Thanks. – abu abu Mar 03 '22 at 02:41
  • 1
    Sure. I have updated my answer. – Reza Saadati Mar 03 '22 at 03:13
  • Thanks @Reza Saadati. I am getting output `undefined`. https://i.stack.imgur.com/Lgy4W.png – abu abu Mar 03 '22 at 03:29
  • 1
    In your case, probably `state.value` is missing. Try `console.log(state)` in your function to see what the output of it is. – Reza Saadati Mar 03 '22 at 11:28
  • Thanks @Reza Saadati. You can check whole code here. http://jsfiddle.net/abufoysal/qjudp87g/1 . Thanks. – abu abu Mar 03 '22 at 11:35
  • 1
    Remove `var` from `var options = [];` (to set it as global). Then replace `templateResult: resultfucntion` with `templateResult: resultfucntion(options)`. – Reza Saadati Mar 03 '22 at 13:41
  • Thanks @Reza Saadati. But how can I use `options` inside `templateResult: resultfucntion(options)`. Did you check my code here ? http://jsfiddle.net/abufoysal/qjudp87g/1/ – abu abu Mar 03 '22 at 14:34
  • 1
    I can't test your code. Please add HTML to it. `templateResult` is the only way that you are trying to invoke the function `resultfucntion()`. Your function requires a parameter, which should be an object, and you are not passing a parameter. Also, `options` is an array. It should be an object instead. – Reza Saadati Mar 03 '22 at 14:41
  • Thanks @Reza Saadati. Actually I am using these whole jQuery code with WordPress. That's why I couldn't add HTML code. Could you please help me in this post ? https://stackoverflow.com/questions/71335358/place-an-html-element-top-of-all-elements .Thanks. – abu abu Mar 03 '22 at 14:54