0

i like to add query string dynamically in in url. the whole table is from jquery Datatable

when i change select box(on change) , i need to add this value to all anchors in td.so i did that, through following code. when i alert href i getting what i want

("XXX?YY=YY&bsort=ZZ")

. but the anchor href's doesn't have that (&bsort=XX) query string(checking by fire bug) whats wrong ?

mycode

$('select[name="category_table_length"]').change(function(){
    var b,href;
    b = $(this).val();
        $('td a').each(function(){
        href = $(this).attr('href');
        href = href+"&bsort="+b; 

        $(this).attr('href',href);
        });
});
6
  • yes it does !. i can get alert inside each Commented Mar 11, 2011 at 11:28
  • Can you test the href content with console.log just before re-assigning it ? (And bring us the result) Commented Mar 11, 2011 at 11:31
  • sorry i'm only getting this(console is not defined) error Commented Mar 11, 2011 at 11:47
  • Ok, use alert("Href = " + href); instead. Commented Mar 11, 2011 at 11:50
  • before assigning :Href =category?id=1&sort=next ...after Href = category?id=1&sort=next&bsort=50 Commented Mar 11, 2011 at 11:52

2 Answers 2

1

It works for me: http://jsfiddle.net/7QTzt/

Is your HTML valid?

BTW, your code doesn't take into account having to remove a "&bsort" property when you select from the dropdown a second time. Perhaps consider a more sophisticated approach.

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

2 Comments

yeah i'm also getting result from alone page. in my case the table from jquery data table is that affect
@gowri: Then the testcase in your question is invalid and you need to provide the actual code that fails.
0

try this example:

HTML:

<select name="category_table_length">
    <option value="1">sort 1</option>
    <option value="2">sort 2</option>
    <option value="3">sort 3</option>    
</select>

<table id="mytable">
    <tr>
        <td><a href="/page.php?data=a">anchor 1</a></td>
    </tr>
    <tr>
        <td><a href="/page.php?data=a">anchor 2</a></td>
    </tr>
</table>

JS:

$('select[name="category_table_length"]').change(function(event){

    var 
        actual_value = $(this).val(),
        new_href = "";

    $('#mytable tr td a').each(function(i, el){

        if (!$(el).attr("original_href"))
            $(el).attr("original_href", $(el).attr('href') )

        new_href = $(el).attr("original_href") + "&bsort=" + actual_value;

        $(el)
            .attr('href', new_href)
            .html("change " + new_href);

    });
});

if you do not have "?" should change to:

new_href = $(el).attr("original_href") + "?bsort=" + actual_value;

3 Comments

yeah i'm also getting result from alone page. in my case the table from jquery data table is that affect .. any idea !
what is the use of .html("change " + new_href);
you could put the html table. the . html( I got to see what worked well, because the links are fake

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.