0

I have a jquery code that is almost complete. I want the new values of the existing parameter to be added on the <a href> using jquery but my code only adds the value at the end of the href.

Sample case: My parameters in the href are food-option AND talent-option.:

<a href="food-option=0&talent-option=0">

Then I have new values to be added for food option.

values: 123,456

My href should be like this:

<a href="food-option=0,123,456&talent-option=0">

This is my code that adds the value:

  $(this).closest(".option-container").find("a.food-form-select-food").each(function () {
        var _href = $(this).attr("href");
        $(this).attr("href", _href + ',' + addnid);
    });

But the result is like this:

 <a href="food-option=0&talent-option=0,123,456">

Which is wrong.

Here is a JSfiddle/Demo of my work

Can somebody please help me?

Thank you very much in advance!

3 Answers 3

4

You are almost there.
Try this:


  $(this).closest(".option-container").
          find("a.food-form-select-food").each(function () {

            var _href = $(this).attr("href").split("&");
            _href[0] +=  ',' + addnid;

            $(this).attr("href", _href.join("&"));

        });
Sign up to request clarification or add additional context in comments.

Comments

0

i think your looking for something like this:

$(this).closest(".option-container").find("a.food-form-select-food").each(function () {
        var _href = $(this).attr("href");
        var n = str.indexOf(",");
        var output = _href.substr(0, position) + addnid + _href.substr(position);
    });

and if you want more answers look at this: Inserting string at position x of another string

Comments

0

try this, JSFIDDLE

$(document).ready(function () {

        $(".select-addon").click(function () {

            var addtitle = $(this).closest('.optionlist').find('.option-title').html();
            var addnid = $(this).closest('.optionlist').find('.option-nid').html();
            var html = '<li class="option-list">' + addtitle + '<div class="addnid2">' + addnid + '</div><span class="remove-addons"> Remove</span></li>';
            $(this).closest(".option-container").find('#your-addons').append(html);

            $(this).closest(".option-container").find("a.food-form-select-food").each(function () {
                var _href = $(this).attr("href");
                var splited = _href.split('&');
                $(this).attr("href", splited[0] + ',' + addnid+'&'+splited[1]);
            });

        });

        $(document).on('click', '.remove-addons', function () {
            var addnid2 = $(this).closest('.option-list').find('.addnid2').html();
            $(this).closest(".option-container").find("a.food-form-select-food").each(function () {
                var href = $(this).attr('href');
                $(this).attr('href', href.replace("," + addnid2, ""));
            });
            $(this).closest('li').remove()
        });

    });
 .option-nid {
        display: none;
    }

    .addnid2 {
        display: none;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="option-container" style="width:200px; float:left">
    <div class="optionlist">
        <div class="option-title">Food Set A</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">132</div>
    </div>
    <div class="optionlist">
        <div class="option-title">Food Set B</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">133</div>
    </div>
    <div class="optionlist">
        <div class="option-title">Food Set C</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">134</div>
    </div>
    <br/>
    <br/>
    <br/>
    <ol id="your-addons"></ol>
    <a href="food-option=0&talent-option=0" class="food-form-select-food">LINK</a>

</div>
<div class="option-container col-md-6">
    <div class="optionlist">
        <div class="option-title">Food Set A</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">132</div>
    </div>
    <div class="optionlist">
        <div class="option-title">Food Set B</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">133</div>
    </div>
    <div class="optionlist">
        <div class="option-title">Food Set C</div>
        <span class="select-addon">SELECT </span>

        <div class="option-nid">134</div>
    </div>
    <br/>
    <br/>
    <br/>
    <ol id="your-addons"></ol>
    <a href="food-option=0&talent-option=0" class="food-form-select-food">LINK</a>

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.