0

I am declaring variable in the click of a link in MVC. Assigning it the value of the "li" text I want to access this variable in my Html page. Or we can say outside the script.

Here is the code snippet.I just want the value of name variable to be accessed on the page.

<script type="text/javascript">

$("a.commentLink").live("click", function () {

                var name = $(this).closest("li").text();
                });
    </script>

Thanks in advance.

5
  • I just edited the above please have a look.Thanks. Commented Apr 14, 2011 at 7:34
  • @Shipla So you have your string. What is it that you want to do? Commented Apr 14, 2011 at 7:36
  • I just need to access this value inside the form tag. Commented Apr 14, 2011 at 7:40
  • @Shilpa My brain knows, but my fingers don't! What do you want to do within the form tag? Commented Apr 14, 2011 at 7:43
  • Its Ok .I want to display this string value on my view page. For example I am having a label and have to display this value within it. Commented Apr 14, 2011 at 7:51

2 Answers 2

0

Place it in an element within your form, e.g.

<form>
    <input type="hidden" id="name" name="name" />

And then in code:

var name = $(this).closest("li").text();
$("#name").val(name);
Sign up to request clarification or add additional context in comments.

Comments

0

mainPage.jsp: Your Script page

<script type="text/javascript">
$("a.commentLink").live("click", function () {
var name = $(this).closest("li").text();
enqueue("demoPage.jsp?nameParam="+name,respAjax);
});
function respAjax(){
  // You can submit or do something or leave it as it is  //
}
</script>

Demo.jsp ----: here you can set value to a Session variable so that you access any were in project or set to a URL parameter

String name = request.getParameter("nameParam");

 response.sendRedirect("mainPage.jsp")
 session.setAttribute("name",name);`enter code here`
 OR
 response.sendRedirect("mainPage.jsp?namPar="+name)

enqueue function can be accessed from ajax file -- For Ajax File : How can I convert a JavaScript variable to a Java variable?

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.