javascript - Jquery - Get element by id constructing the id in string -


i have trouble using element jquery. constructing name in var such as:

var myid = "#" + mygotid;  $(myid).attr("title", "changed"); 

$(myid) returning empty. want element id constructing id dynamically joining strings.

edit @pointy — additional code supplied in comment op:

var form = $('form#formdefaultvalue'); $(':submit', form).click(function(event) {   event.preventdefault();   $.post(form.attr('action'), form.serialize(), function(data, status, xmlhttprequest) {     if (status == 'success') {       $("#msginformation").html("your changes have been saved.");       jquery("#spandefaultvalue").attr("class", "db");       var g = indexnodeselected;       g = g.replace("\\", "\\\\\\\\");       $('#'+ g).find("span").attr("class", "");    }  

i don't know happening because have not posted code, make sure javascript code running after element target "id" value has been included in dom. if have this:

<html>   <head>     <script>       var myid = '#' + mygotid;       $(myid).whatever(); 

then won't work because actual page not have been seen when code runs.

instead, try this:

  <script>     $(function() {       var myid = '#' + mygotid;       // ...     }); 

it important make sure "mygotid" string value expect (that is, value coded "id" attribute of target element).

edit if think you're correctly constructing "id", try alternative:

$(document.getelementbyid(mygotid)).whatever() 

in other words, can "wrap" plain dom element in jquery object passing $() function. if works, problem "id" value somehow confusing selector engine. "id" value contain "." chance?


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -