0

This is my jquery function the argument passed is php json_encoded data,

   function html_modify(html) {
         var str=JSON.stringify(html);
         var arr=$.parseJSON(str);

         var tot='';
         $.each(arr,function(i,val){

            if(i=='options') {
               alert(val);
            }
         });

    }

  html_modify(<?php echo $encode ?>);

The value I will get on alert that is alert(val) will be,

       <span style="font-family: Times New Roman; font-size: 12pt;"><img style="vertical-align:middle" width=83 height=44 src="image045.gif"></span><span style="font-family: Times New Roman; font-size: 12pt;"><img style="vertical-align:middle" width=48 height=44 src="image046.gif"></span><span style="font-family: Times New Roman; font-size: 12pt;"><img style="vertical-align:middle" width=48 height=44 src="image047.gif"></span><span style="font-family: Times New Roman; font-size: 12pt;"><img style="vertical-align:middle" width=83 height=44 src="image048.gif"></span>

I am trying to remove all the "spans" present in the result above and keep only "image" tags further. But currently have no clue. Thanks.

2 Answers 2

3

I am trying to remove all the "spans" present in the result above and keep only "image" tags further.

So for this you have to find image in val and .unwrap() it:

if(i=='options') {
   var elem = $(val).find('img').unwrap();
   alert(elem);
}

A small demo to you.

Sign up to request clarification or add additional context in comments.

6 Comments

oh you have to do val.find().unwrap() its updated now.
"TypeError: val.find is not a function" is the result i got.
make it a jquery object then. $(val). yes this should work now.
I am getting alert as [object Object] if I use $(val). thanks anyway.
yes, in console it is printing the object,but what i am getting is object, is there any way to turn it into html like "<img src='image1.jpg'> </img>" so that i can append a "<input type='radio'>" and prepend "</input>" to the images.thanks
|
1

You can manipulate the HTML with JQuery but first you should wrap it in a DIV element so that JQuery will be able to iterate over the child elements properly:

var div = $("<div/>").append(val);//create a JQuery object for searching
var result = $("<div/>");//build our result JQuery object

Then you can loop each span:

//loop each span element
div.find("span").each(function(){
    var img = $(this).find("img");//find the img element for the span
    result.append(img);//append the img element to the result object
});

and get your final HTML result:

var finalHtml = result.html();//convert the result to html string

Here is a working example

8 Comments

Really sorry for not looking into your to the point answer, however i was initially confused with <div/> tag as I have not used it before let me check it thanks.sorry again.
@echoLearner_php: Just ask if you are unsure about anything, and don't get me wrong as Jai's answer using unwrap is a much simpler solution so you can still use that. The <div/> used here just creates a parent dummy element that we can search, and also call html() on to get the contents. It is just there for the process, it gets discarded when we are finished anyway
thanks it was working,but if wanted to include each image within a radio button how would I do that?
var radio = $("<input type='radio' name='choice' value='"+tot+"'/>"); radio.append(img); result.append(radio);
@echoLearner_php: No idea why it's not working, might be something to do with invalid HTML. Here is a work around though
|

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.