[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

jquery variable as identifier

Posted on 2011-09-10
6
Medium Priority
?
445 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
[X]
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
  • 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:Gurvinder Pal Singh
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

650 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