0

I have a list of templates html. onClick of anchor tag, HTML should be fetched and display as output which is equal to this attribute id.

Eg: If I click of 01 link, output should be Content 01, 02 should bring Content 2 etc...

Currently I am getting clicked rp-wp-template-id as wpt_bars_1 etc... instead I want to get the varibale html which is equalent to this id.

Desired output should be: Output: Content 1, 2 etc...

jsFiddle

jQuery(document).on('click', '.wp-templates-list li a', function(e) {
  var _self = jQuery(this);
  var _wp_template_id = _self.attr('rp-wp-template-id');
  var wpt_bars_1 = 'Content 1';
  var wpt_bars_2 = 'Content 2';
  var wpt_bars_3 = 'Content 3';
  var wpt_bars_4 = 'Content 4';
  var wpt_bars_5 = 'Content 5';
  jQuery('#getHtmlContainer').html('Output: ' + _wp_template_id);
});
body {
  font-family: verdana;
  font-size: 13px;
}

li {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  display: block;
  color: #0066cc;
  background-color: #fefefe;
  border: 1px solid #ccc;
  padding: 5px;
  width: 80%;
  margin: 0 auto 20px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="wp-templates-list">
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_1">01</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_2">02</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_3">03</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_4">04</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_5">05</a></li>
</ul>

<div id="getHtmlContainer">Output: </div>

1 Answer 1

2

You need an object

You can also get closer to the LI in your delegation

const bars = {
  'wpt_bars_1': 'Content 1',
  'wpt_bars_2': 'Content 2',
  'wpt_bars_3': 'Content 3',
  'wpt_bars_4': 'Content 4',
  'wpt_bars_5': 'Content 5',
}

$('.wp-templates-list').on('click', 'li a', function(e) {
  var _self = $(this);
  var _wp_template_id = _self.attr('rp-wp-template-id');
  $('#getHtmlContainer').html('Output: ' + bars[_wp_template_id]);
});
body {
  font-family: verdana;
  font-size: 13px;
}

li {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  display: block;
  color: #0066cc;
  background-color: #fefefe;
  border: 1px solid #ccc;
  padding: 5px;
  width: 80%;
  margin: 0 auto 20px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="wp-templates-list">
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_1">01</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_2">02</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_3">03</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_4">04</a></li>
  <li><a href="javascript:;" rp-wp-template-id="wpt_bars_5">05</a></li>
</ul>

<div id="getHtmlContainer">Output: </div>

Sign up to request clarification or add additional context in comments.

1 Comment

@mplugjan, thanks for the perfect answer... I am accepting and upvoting your answer. Thanks for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.