I'm facing a problem in using the HTML code contained in a javascript variable with the DOM methods like getElementsByTagName("img"), .removeAttribute("alt"), etc. I want to remove the alt attribute from all the <img> tags present within the HTML code and having alt attribute. Following are few lines of my code:

 $(".show_sub_questions").click(function() {  
        var sub_id = $(this).attr('id'); 
        $('#'+sub_id).removeClass("show_sub_questions");
        var subject_name = $('#'+sub_id).attr('class');
        $('#'+sub_id).addClass("show_sub_questions");

        **var data = $('#popup_'+sub_id).html();**

var title = "Test Result";
        var dialog_title   = title; 
        var subject_name = "<div class='s_name'><b>Subject Name : </b>"+subject_name+"</div><br />";             
        var dialog_message = subject_name+data; 
    var $dialog = $("<div class='view_result'></div>")
         .html(dialog_message)
         .dialog({
               autoOpen: false,
               modal:true,
               title: dialog_title,
               width: 900,                     
               close:{
               }
         });
         $dialog.dialog('open');

        return false; 
    }); 

I'm getting the HTML code from the .tpl file which is added to the popup. Please help me out from this issue of accessing html code with DOM methods. The step written within ** and ** is the HTML code giving step. I want to use DOM methods on the same HTML code.

share|improve this question
To your first problem: getElementsByTagName returns an array, so you have to use a for loop. To your marked code: varr data = document.getElementById("popup_" + subid); – Tearsdontfalls Jan 3 at 7:22
I agree with you @Tearsdontfalls. I even used the for loop, following is the code: var x=document.getElementsByTagName("img"); for (var i=0; i<x.length; i++) { data.getElementsByTagName("img")[i].removeAttribute("alt"); } But it's giving error in a function. If I use document.getElementsByTagName("img")[i].removeAttribute("alt"); it's not giving error but the output is nothing. Why? I don't know. – Sushil Jan 3 at 7:28
Maybe you should use x[i] in the for loop, data is maybe undefined – Tearsdontfalls Jan 3 at 7:42
Still the output is same, no error and no removal of alt attribute. Could there be any better way @Tearsdontfalls? – Sushil Jan 3 at 7:51
Maybe . SetAttribute("attr", ""); is an option – Tearsdontfalls Jan 3 at 9:36
feedback

1 Answer

Actually I can't find the code where You are removing alt's from imgs but try something like:

$('#parent_element img').removeAttr('alt');

In this case You don't have to keep HTML in variable. It will be changed on fly. You can allways made an invisible element (ie style="display: none;") and put the code to it. Chenge HTML, read it again and remove fake element. May not be the best solution but it will work ad give You some more option. You can use all the time on element to do operations like that and clear it every time instead of removing.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.