views:

289

answers:

2

Here's my jquery method:

$.ajax({
    type: "GET",
    url: "Home/GetSocialMentionBlogs/"
    dataType: "json",
    success: function (data) {
        var obj = JSON.parse(data);
        $.each(obj.items, function (i, item) {
            $("<strong><p>" + item.title + "</strong></p>").appendTo("#blogs");
            if (i == 5) return false;
        });
    }
});

What I want to do is when a user clicks a button, callt his method, and pass in the value of a textbox so that the URL will now be:

url: Home/GetSocialMentionBlogs/value from text box

Of course I'll need to URL encode that, but as of now, I don't know how to pass in values to this .ajax function.

I'm completely new to jQuery and MVC so pardon my ignorrance ont he subject so far.

+2  A: 
$.ajax({
    type: "GET",
    url: "Home/GetSocialMentionBlogs/" + $('#textBoxID').val(),
    dataType: "json",
    success: function (data) {
        var obj = JSON.parse(data);
        $.each(obj.items, function (i, item) {
            $("<strong><p>" + item.title + "</strong></p>").appendTo("#blogs");
            if (i == 5) return false;
        });
    }
});
ctrlShiftBryan
"rooted"? what do you mean?
Pointy
+3  A: 

Well if the input field has an "id" value you'd just do

url: "Home/GetSocialMentionBlogs/" + $('#inputFieldId').val(),

If all you've got is the name, then you could do:

url: "Home/GetSocialMentionBlogs/" + $('input[name=inputFieldName]').val(),

Is that all you need to do or am I missing some detail?

Oh and to URL encode just use the Javascript encodeURIComponent() function.

Pointy
+1 for alternate way of selecting the input field.
Wadih M.
+1 But I edited it and changed to use `encodeURIComponent` instead of `escape` http://xkr.us/articles/javascript/encode-compare/
Josh Stodola
@Josh thanks! Also here's a link I found: http://xkr.us/articles/javascript/encode-compare/ [edit] oh ha! well thanks again!
Pointy
Add a "click" event handler to the button, and make sure it returns "false". That'll prevent the native action (submitting the form) from happening. The jQuery API docs for event handling are some good reading: http://api.jquery.com
Pointy