1

In fact, I am working on a small PHP script and I use this code to make an ajax query

 var CODE = $('.code').val();
         var CASE = $('.code').attr('case');
  $.ajax({
            type:'POST',
            url:'ajax.php',
            data:'code='+CODE+'&case='+CASE,
            success:function(html){

            }
        });

the response is like that

<span class="name">Yassine</span>
<span class="email">[email protected]</span>

I want to store those results as javascript variables like this:

Var name = Yassine
Var email = [email protected]

1 Answer 1

1

You can use jQuery(), .eq(), .text()

var response = $(html);
var name = response.eq(0).text();
var email = response.eq(1).text();
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work :( The Html responses are SPANS HTML tags.
@YassineDevlopper Can you describe "It doesn't work"? "The Html responses are SPANS HTML tags" Yes
The response is like this : <span class="name">Yassine</span> I want to Put Yassine as variable so I'm looking for a way to select the span by using the class name and get interior value . Any other way will be OK.
@YassineDevlopper "The response is like this : <span class="name">Yassine</span> I want to Put Yassine as variable so I'm looking for a way to select the span by using the class name and get interior value ." Yes, js at post should achieve expected result; see jsfiddle jsfiddle.net/16nq024f
@YassineDevlopper You could alternatively use .filter() to select element by className, e.g.; response.filter(".name").text() , see jsfiddle.net/16nq024f/2

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.