0

I want to declare an array when I am declaring a JQuery function. How do I do that?

If it is possible then how can I pass items to that array in an OnClick event?

For Example:

 Function EditGrid(Declare a Array Here?)
{
//Do something…
}

<a href=”javascript:void(0);” onclick=’EditGrid(‘[CategoryName1,CategoryName2,CategoryName2….etc ]’)’>Edit Item</a>

1 Answer 1

1

I don't see any jQuery in your example.

If I understand correctly, all you want to do upon clicking on a link/button is to call a function and give it an array, then edit your grid from that.

html:

<button>Edit item</button>

javascript:

function editGrid(array){
    array.forEach(function(item){
        alert(item);
    });    

    // Edit your grid
}

$(function(){
    $("button").click(function(){
        editGrid(['categoryName1', 'categoryName2']);
    });
});

Jsfiddle DEMO

Let me know if that helps.

Edit:

You could call the editGrid function with different parameters based on which button is clicked.

View my updated Jsfiddle DEMO.

$("#button1").click(function(){
    editGrid(['categoryName1', 'categoryName2']);
});

$("#button2").click(function(){
    editGrid(['categoryName3', 'categoryName4']);
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is not exactly what i am looking for. I just want to create a function with array object as a parameter. then i need to pass values to that parameters on button/ anchor tag click event.
I edited my post. I believe this to be exactly what you need. You can pass whatever parameters you want based on which button it is. This could also work with links. Let me know if that helps
editGrid(['categoryName3', 'categoryName4']); in this CategoryName3, CategoryName4....etc are coming from server script. that is the reason i can't use these values at client script.
Kinda hard to help you when you barely provided any explanation. You could retrieve your data from the server with an ajax call (using $.ajax). Good luck.

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.