Search code examples
javascriptjquerysmarty

How to concatenate a new value from an existing attribute value using Jquery?


sorry for the question title if it confusing to you. My simple problem is I have an HTML element with an attribute. But I want to concatenate a new value using an event which is onclick. After the user clicked the link the element attribute will be concatenated with my new value coming from my jquery value.

Here's the sample code. By the way I am using smarty.

<a href="products.search.advance&search_by=" class="w024 h024 ml006 b08 u003 d2 f1" id="call_dispatch">

$("#call_dispatch").click(function()
    {ldelim}

        var dispatch_value = $("#dispatch").val();
        var menu = dispatch_value + "|fn_url";

        $('#call_dispatch').attr('href',menu);

        var x = $('#call_dispatch').attr('href');
        alert(x);

    {rdelim}    
);

Now as you have seen in the href attribute I have a search by value. Now my problem is I need to concatenate this with the var menu value in my jquery. But I don't know how. Please help me guys. Thanks.


Solution

  • There is another way to do it. You have to append thet search text to your anchor tag's href when you change the selection.

    consider you have select options like below

    <select id="dispatch">
        <option value="first">first</options>
        <option value="second">second</options>
        <option value="third">third</options>
    </select>
    

    and your anchor tag is

    <a href="products.search.advance&search_by=" class="w024 h024 ml006 b08 u003 d2 f1" id="call_dispatch">
    

    now use the following code

    $("#dispatch").change(function(){
    
    var dispatch_value = $("#dispatch").val();
    
    var menu = dispatch_value + "|fn_url";
    
        $('#call_dispatch').attr('href',menu);
    
        var x = $('#call_dispatch').attr('href');
        alert(x);
    

    });

    i created a fiddle. please look at this: http://jsfiddle.net/codingsolver/bJ4nw/1/

    hope it helps you.