0

I'm using jquery ajax to fetch a page called matid.jsp like this::

var loadUrl = "matid.jsp";


          $(".get").click(function () {
              $.get(loadUrl, function (responseText) {
                  $("#result").html(responseText);
              });
          });

Is there a way to update this var loadurl from the body of the html page to look like this

"matid.jsp?hello" the next time a click is made?

Below is how I create multiple divs that should have different loadurl parameter when the .get() function is called

for(int i=1; 1<20; i++){
<div onclick="$.get()" class="get"><%=i%> </div>
}

Thanks

princesden
  • 15
  • 1
  • 5

1 Answers1

1

Umm, you can add data attributes on your div, these were introduced in HTML5. You can use jQuery to update/change the data-url attribute inside the click function if you need now. Hope this helps. the JavaScript

$(".get").click(function(){

   // get the page associated with the div element
   // the below line gets the value of the data-url attribute
   var page = jQuery.data($(this), "url");

   // load the page with a GET request
   $.get(page, function(responseText){
      // set the result
      $(".result").html(responseText);
   });
});

the HTML

<div class="get" data-url="somepage.php?page=1"></div>
<div class="get" data-url="somepage.php?page=2"></div>

<div class="result">
</div>

For your reference on data attributes:

In HTML: http://ejohn.org/blog/html-5-data-attributes/

In jQuery: http://api.jquery.com/jQuery.data/

Good luck.

Saad Imran.
  • 4,480
  • 2
  • 23
  • 33
  • You can have custom attributes and retrieve them in JavaScript, even in the old browsers. They just weren't standard compliant before, they became part of the standards in HTML5. So that should work fine even in IE8 or IE6. Here's a link: http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6 – Saad Imran. Aug 03 '11 at 13:54