Solved

jquery variable as identifier

Posted on 2011-09-10
6
435 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

757 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