Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery equivalent to `return document.getElementById(theVar)'

I've a javascript function

function $m(theVar){
    return document.getElementById(theVar)
}

The problem with the above code is, the element I want to pass is generated by ajax so I need something like the .live() in jquery, unless it throws an error

How do I rewrite the above function in jQuery so that any DOM element generated later can also be detected.

Update:

When my page first load, it loads

1) ajaxupload.js with codes

function ajaxUpload(form,url_action,msg){
   var id_element= "pop-con";
   function $m(theVar){
        return document.getElementById(theVar)
    }
   if($m(id_element)==null){
        erro += "The element of 3rd parameter does not exists.\n";
    }
}

2) index.php with codes

<div id="popupMargin">
    <div class="close">x</div>
    <div id="pop-con"></div>
</div>
<div id="ajaxGenerateMarkUp"></div>

3) now on the click of a button, the following markUp is added to the #ajaxGeneratedmarkUp div (mark-up generated through ajax)

<form  onSubmit="return disableForm(this);" action="crop/wizecho_upload.php" method="post" name="f" id="wizecho" enctype="multipart/form-data">
      <input id="file" type="file" name="file"  onChange="return disableForm(this), ajaxUpload(this.form,'crop/wizecho_upload.php', '&lt;br&gt;Uploading image please wait.....&lt;br&gt;'); return false;"/>
</form>

Now on change of this input type file, made the call on in the javascript. Now it shows the error.

[Note: I only posted sections of code I think might affect my question]

like image 293
ptamzz Avatar asked Jan 27 '26 22:01

ptamzz


1 Answers

Like this:

return $('#' + theVar)[0];
like image 182
Yaakov Shoham Avatar answered Jan 29 '26 12:01

Yaakov Shoham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!