Home > Back-end >  get value of custom attribute from dropdown to button or link
get value of custom attribute from dropdown to button or link

Time:06-22

I need get "perma" value to button when is selected option in dropdown menu.

I have this HTML:

<div >
<div><a href="#3303" perma="http://test.com/xxxx/"></a></div>
<div><a href="#3304" perma="http://test.com/xxxx2/"></a></div>
<div><a href="#3305" perma="http://test.com/xxxx3/"></a></div>
<div>

When is selected some div, perma attribute should somehow write into this :

<a href="perma">button</a>

Dropdown list Jquery:

 //.input-dropdown
    jQuery('.input-dropdown input, .input-dropdown > i').on('click',function(){

        var elThis = jQuery(this);
        var elParent = elThis.parents('.input-dropdown');
        var elDropdownList = elParent.find('.dropdown-list');
        
        if( elDropdownList.is(':visible') ){
            elDropdownList.stop(true, true).slideUp(300);            
        }else{
            elDropdownList.stop(true, true).slideDown(300);            
        }
                
    });
    jQuery('.input-dropdown .dropdown-list a').on('click',function(e){
        e.preventDefault();
        
        var elThis = jQuery(this);
        var elParent = elThis.parents('.input-dropdown');
        var elDropdownList = elParent.find('.dropdown-list');
        var elInput = elParent.find('input');
        
        elInput.val(elThis.data('value'));

        elDropdownList.stop(true, true).slideUp(300);
        
    });

    jQuery(document).click(function(event) { 
        if(!jQuery(event.target).closest('.input-dropdown').length) {
            if(jQuery('.input-dropdown .dropdown-list').is(":visible")) {
                jQuery('.input-dropdown .dropdown-list').stop(true,true).slideUp(300);
            }
        }        
    });

What i tried:

<script>function gotosite() {
  window.location = document.getAttribute("perma").perma; 
}</script> <button id="go" onclick="gotosite()">Go</button>

<a href="<?php echo $row['perma'];?>">button</a>

CodePudding user response:

With your dropdown list and button

<div >
    <div><a href="#3301" perma="http://test.com/xxxx1/">Link 1</a></div>
    <div><a href="#3302" perma="http://test.com/xxxx2/">Link 2</a></div>
    <div><a href="#3303" perma="http://test.com/xxxx3/">Link 3</a></div>
  </div>
<a id="myButton" href="http://test.com/default/">button</a>

You can use Jquery to set href value of your button

$('.dropdown-list div a').click(function () {
    //console.log($(this).attr('perma'));
    //console.log($(this).attr('href'));
    $("#myButton").attr("href", $(this).attr('perma'))
});
  • Related