Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

jquery variable as identifier

Posted on 2011-09-10
6
439 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

809 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