views:

496

answers:

4

I am new to jQuery and I have the following problem.

My project has say 2 pages, 1.JSP and 2.html. Now I want to pick selected data from 2.html and use it on 1.JSP. Now this was achieved very easily using .load but I want the data to be present in a JavaScript variable rather than put it on the page (div tags, etc.), so that I can work upon that data (modify or add to database).

I tried using .ajax and was able to write the following code:

    var value = (function () {
        var val = nulll;
        var filename = " 2.html";

        $.ajax ({
            'async': false,
            'global': false,
            'url': filename,
            'success' : function(data) {
                val = data;
            }
        })
        return val;
    })()
    document.write(value)

Where do I put the selector format (say div.id5) so that my variable have only relevant data rather than the full file data?

+1  A: 
function modify_data(data){
....
}

$.ajax({
   type: "POST", //POST OR GET
   url: "1.JSP", // URL TO SEND AJAX DATA TO
   data: "name=John&location=Boston", // DATA TO SEND
   success: function(data){ // CALLBACK FUNCTION
     modify_data(data);// sending data to another function to play with it
     $('body').html(data);
   }
 });

This is how to send an Ajax request and print it on the body after modifying the received data.

From.ME.to.YOU
I can not add anything on 2.html page. I just have 2.html file which is well formed( and i m not supposed to change anything or add anything in it.) Now my problem is that I want to retrieve specific data of some tags(from 2.html) in Javascript variables on 1.JSP page( so that i can add that particular info in database(say)).a simple example of 'p.hello' tag from 2.html page to 1.jsp page will help.Thanks.
A: 

I can not add anything on 2.html page. I just have 2.html file which is well formed( and i m not supposed to change anything or add anything in it.) Now my problem is that I want to retrieve specific data of some tags(from 2.html) in Javascript variables on 1.JSP page( so that i can add that particular info in database(say)). a simple example of 'p.hello' tag from 2.html page to 1.jsp page will help.

A: 

Let's say you have 1.html and 2.html, and inside 2.html's body you have this:

<body>
<h1 id="foo">hello</h1>
</body>

Then in your 1.html you'd get the text inside H1 like this:

var filename = " 2.html";

$.ajax ({
    'async': false,
    'global': false,
    'url': filename,
    'success' : function(data) {
        var html = $(data);
        html.each(function() {
            if(this.tagName && this.tagName == 'H1') {
                alert( $(this).html() ); 
            }
        })
    }
})

This should give an annoying JS alert with 'hello' written inside it.

dalbaeb
this code is not working ..... more help required.
Works for me in IE8 and Firefox. Are you including jQuery before this code? <script type="text/javascript" src="jquery-1.4.2.js"></script>
dalbaeb
A: 

I guess you're referring to $.load() function's ability to receive a jquery selector along with the URL to filter the result. That's to say, today you're doing it like this:

$('div').load('2.html div.id5');

So you really want to be able to do the same using the $.ajax() function. I believe the easiest way for you to do that is to use the .find() jquery function inside your 'success' event function (I'm omitting the whole .ajax() call, just typing the success event code):

success: function (data) {
    val = $(data).find('div.id5').html(); 
    // now you ony have that specific div's contents
}

I saw a few other errors in your javascript code I think you'll want to fix (if you're actually using that code).

    // null is defined with 2 ls
    var val = nulll;

    // Is the space on purpose?
    var filename = " 2.html";
arnorhs