Solved

jquery variable as identifier

Posted on 2011-09-10
6
436 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now