Ashish Kacha
Ashish Kacha

Reputation: 39

**ERROR:** "Uncaught TypeError: Cannot read property 'replace' of undefined"?

ERROR: "Uncaught TypeError: Cannot read property 'replace' of undefined"?

jQuery(document).ready(function(){
    	var rep = jQuery(".abc")
              .clone()
              .wrap("<div></div>")
              .parent().html()
              .replace(/select/g,"ul")
              .replace(/option/g,"li");

    	jQuery(".abc").replaceWith(rep);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Views: 585

Answers (1)

Raphael Schweikert
Raphael Schweikert

Reputation: 18556

I think this works as intended:

jQuery(document).ready(function(){
	var rep = jQuery(".abc")
          .clone()
          .wrap("<div></div>")
          .parent().html()
          .replace(/select/g,"ul")
          .replace(/option/g,"li");

	jQuery(".abc").replaceWith(rep);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="abc">
  <select>
    <option>1</option>
    <option>2</option>
  </select>
</div>

Maybe you’re missing a .abc element on your page. In that case .html() will return undefined.

Upvotes: 1

Related Questions