0

My Javascript code is appearing within a dropdown menu (example URL is at the bottom of this post). I thought the browser would ignore valid HTML, but that doesn't seem to be happening in this example.

Some context: I have a Wordpress site and added Javascript code that adds the price variables next to the item names on a dropdown menu. Then I tried applying some CSS to the Javascript output (color red, float right, padding) so that the variation prices were easily seen next to the item names.

Here's the code:

function display_price_in_variation_option_name( $term ) {
    $product = wc_get_product();
    $id = $product->get_id();
    if ( empty( $term ) || empty( $id ) ) {
        return $term;
    }
    if ( $product->is_type( 'variable' ) ) {
        $product_variations = $product->get_available_variations();
    } else {
        return $term;
    }

    foreach($product_variations as $variation){
        if(count($variation['attributes']) > 1){
            return $term;
        }
        foreach($variation['attributes'] as $key => $slug){
            if("attribute_" == mb_substr( $key, 0, 10 )){
                $taxonomy = mb_substr( $key, 10 ) ;
                $attribute = get_term_by('slug', $slug, $taxonomy);
                if($attribute->name == $term){
                    $term .= "&nbsp;&nbsp; <span style=\"color: red; float: right; padding: 0% 7% 0% 0%;\"> &nbsp;&nbsp;" . wp_kses( wc_price($variation['display_price']), array()) . "&nbsp; </span> &nbsp;";
                }
            }
        }
    }
    
    return $term;

}

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

But then my in-line CSS code is appearing in the dropdown menu.

Here's an example URL of what's happening:

http://66.228.41.220/product/v-neck-t-shirt/

On that page, click on that dropdown menu and notice the <span> HTML code appears.

So then I tried to put the Javascript function into an element, so that I can access it in my style.css. Using this method:

https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style

But I can't seem to do it successfully. What am I missing here?

ikiK
  • 6,328
  • 4
  • 20
  • 40
  • Does this answer your question? [is it possible to add
    or inside an
    – ikiK Apr 25 '21 at 21:27
  • I appreciate the help. Unfortunately, it says that those solutions don't work anymore as of 2019. Here's a Youtube cideo showing exactly what's happening: https://www.youtube.com/watch?v=gwzOnyesoSk Any help that anyone has would be appreciated. I'm stuck. – WaltRibeiro Apr 26 '21 at 02:57

1 Answers1

0

The html code you are seeing is due to woocommerce applying esc_html() to your output. It converts things like < into &lt;.

In the specs for the option element it states that the only permitted content is:

Text, possibly with escaped characters (like &eacute;).

So you cannot have any tags in an option element. You can try this javascript for example:

$('#pa_color option').each(function(i, optionEl){
    var optionText = $(optionEl).text();
    var startIndex = optionText.indexOf('$');
    if(startIndex !== -1){
         var priceText = optionText.substr(startIndex);
         var restOfText = optionText.substring(0,startIndex);
         var output = restOfText + '<span style="color: #565656; float: right; padding: 0% 7% 0% 0%;">' + priceText + '</span>';
         $(optionEl).html(output);
    }
});

It works as you can see in the developer console, but it doesn't show on the page. The browser just ignores the tag...

<select>
<option><span style="color:red;">first and only option</span></option>
</select>

If you really need this functionality you should write your own select element using only divs, or you could try and find a plugin that does it for you like these ones.

Klaassiek
  • 2,795
  • 1
  • 23
  • 41
  • This is a big help, and some of those plugins are pretty close to what I need. Maybe with some tweaking then I can change the color of a specific word in the string. I'm considering writing a CSS regular expression that changes the color of the first 4 characters that follow the "$" symbol in my drop down menu. I have a few ideas with your suggestions. I'll let you know how it works out. Thanks so much. – WaltRibeiro Apr 26 '21 at 03:13
  • Glad I could help. Please consider upvoting and marking this as answered. :) – Klaassiek Apr 26 '21 at 03:48
  • I'll make it the answered post because it got me further in my understanding what's happening underneath. Thanks. – WaltRibeiro May 03 '21 at 00:31