Solved

jquery variable as identifier

Posted on 2011-09-10
6
437 Views
Last Modified: 2012-05-12
Hello

I have this function :

function removeelement(element){
      jQuery.("#WishList").remove("#"+element);
}

I need that variable 'elemnt' to be interpreted as name of a div

Thanks
0
Comment
Question by:radugheorghies
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36516864
name

Open in new window

can only be set on form elements : input, textarea, form, select
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36516866
it should be id attribute, not name
http://api.jquery.com/id-selector/
0
 

Author Comment

by:radugheorghies
ID: 36516915
Thanks gurvinder372.

I have modified my function:

function removeelement(element){
      //jQuery.("#WishList").remove("\"#"+element+"\"");
      alert(element);
      var d = document.getElementById('WishList');
      var olddiv = document.getElementById(element);
      d.removeChild(olddiv);
}

Now I have an error : var olddiv = document.getElementById(element); olddiv is null.
The element div is added dynamically

Any ideea?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36516949
javascript : http://jsfiddle.net/mVHjx/

function removeelement(element){
      var divs = document.getElementsByTagName("div");
    for(var i=0;i<divs.length;i++) {
        var div = divs[i];
        if( div.getAttribute("name") == element ) {
            div.parentNode.removeChild(div);            
        }
    }
}

alert("before");
removeelement("_e");
alert("after");

Open in new window


jQuery : http://jsfiddle.net/c8vKC/2/

function removeelement(element){
    $("div[name='" + element + "']").remove();
}

alert("before");
removeelement("_e");
alert("after");

Open in new window


http://www.w3schools.com/TAGS/att_input_name.asp

Definition and Usage

The name attribute specifies a name for an input element.

The name attribute is used to identify form data after it has been submitted to the server, or to reference form data using JavaScript on the client side.

Note: Only form elements with a name attribute will have their values passed when submitting a form.
0
 

Author Comment

by:radugheorghies
ID: 36516990
Thank you leakim971

I used your function in this way :
function removeelement(element){
    $("div[id='" + element + "']").remove();
}

When i said 'name' I meant 'ID'. My mistake.

Thanks again.
0
 

Author Closing Comment

by:radugheorghies
ID: 36516991
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question