I have the following scripts:
<script>
window.onload = function setHref(){
var affglcid = <?php echo json_encode($kws); ?>;
var oldLink=document.getElementById('link').href;
document.getElementById('link').setAttribute('href', oldLink+affglcid);
var oldLink1=document.getElementById('link2').href;
document.getElementById('link2').setAttribute('href', oldLink1+affglcid);
}
</script>
And:
<a id="link" href="oursite.com/">Link</a>
<a id="link2" href="othersite.com/">Link</a>
First, it takes a PHP variable:
var affglcid = <?php echo json_encode($kws); ?>;
Then it appends the variable at the end of a link:
var oldLink=document.getElementById('link').href;
document.getElementById('link').setAttribute('href', oldLink+affglcid);
It should do the same for another link:
var oldLink1=document.getElementById('link2').href;
document.getElementById('link2').setAttribute('href', oldLink1+affglcid);
So, if $kw=xy then the first link should be "oursite.com/xy" and the second one "othersite.com/xy" but it only works on the other site link. The code used for that is the following:
<a id="link" href="oursite.com/">Link</a>
<a id="link2" href="othersite.com/">Link</a
Any ideas what's wrong?
I noticed that your URLs not correct, they should start with "http://" or "https://"
<a id="link" href="http://oursite.com/">Link</a>
<a id="link2" href="http://othersite.com/">Link</a>
Even if you don't want to add protocol, you should atleast use //oursite.com, otherwise it'll be considered as relative URL which will append this URL in front of your current URL and try to open that
Related
This is wordpress plugin code The following code works, but no text comes into the modal window. Error is this enter image description here
<?php
$phpvariabletext = get_post_meta( $post_id, '_billing_satis_sozlesme', true );
// $phpvariabletext large text along with html codes
?>
<a class="classa" id="view" onclick="openmodal()"> Show Contract </a>
<script type="text/javascript">
var content = '<?php echo $phpvariabletext; ?>';
var newmodal = new tingle.modal();
function openmodal() {
newmodal.open();
newmodal.setContent(content);
}
</script>
You can use PHP variable in JQuery/Javascript easily check below steps
1) Stored PHP variable value in HTML input tag as type hidden
<input type="hidden" value="<?php echo $phpvariabletext;?>" id="phpvariable">
2) After assign variable value in HTML input tag. You can get value in JQuery/Javascript.
<script type="text/javascript">
var content = $('#phpvariable').val();
var newmodal = new tingle.modal();
function openmodal() {
newmodal.open();
newmodal.setContent(content);
}
</script>
Creating extra DOM elements solely to transport a value is needless extra markup.
I always pass php data to javascript as json_encode() output (without manually quoting) because it will automatically double-quote your string and escape any of the double-quotes in the actual data.
<a class="classa" id="view" onclick="openmodal()"> Show Contract </a>
<script type="text/javascript">
let newmodal = new tingle.modal();
function openmodal() {
newmodal.open();
newmodal.setContent(<?= json_encode(get_post_meta($post_id, '_billing_satis_sozlesme', true)); ?>);
}
</script>
The above technique outputs the php value using php "short tags".
P.s. you could also consider removing the inline js function call and use an event listener in your js to keep all of the behaviors/events in one place.
How can i bind the url result from the jquery to the href ? as you can see on the html below. Thank you.
JQUERY CODE
<script>
$(document).ready(function(){
$("#btnSearch").click(function(){
var yearArray = [];
$("input:checkbox[name=Year]:checked").each(function(){
yearArray.push($(this).val());
});
var makeArray = [];
$("input:checkbox[name=Make]:checked").each(function(){
makeArray.push($(this).val());
});
var url="http://example.com/results?Year="+yearArray.join()+"&Model="+makeArray.join();
alert(url);
});
});
</script>
HTML
<a href="/searchnew/{{ url }}"><button id="btnSearch" class="btn btn-cta margin-bottom-1x search-button cssBase"
type="button""
style="width: 262.5px;">Search</button></a>
You can simpy add href attribute after creating link. What i mean by this is
if your href tag is look like this
<a href="#" id="myurl">
$("#myurl").prop("href",url);
This will simply render your html like this
<a href="http://example.com/results?Year="......(your dynamic values here)">
You don't have to add tag on to button click. Instead, on click of button, within the function after you have prepared url, call
location.href = "searchnew/" + url;
In case you have the base url for the app, you can prepend as well to have the complete url with you.
Use id attribute for your a tag like this-
<a href="....." id="myLink" > ... </a>
And at the end of your jQuery code add the following:
var a = $('#myLink');
var href = a.attr('href');
a.attr('href', href + url);
I do not know how to find a string in the html tag and replace it with another one.
I will show it on an example.
I have something like that
<a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
And I would like to get such an effect
<a class="test"> <img-src="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
Does anyone have any idea? I'm sorry for my level of English.
You can first append the img and then remove the attribute data-value.
See the code below:
$("a").append(function(){
return '<img src="'+$(this).attr("data-value")+'">';
}).removeAttr("data-value")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
Assuming that you are new in JavaScript and/or jQuery, I've made a step-by-step example of a solution:
// create a pointer to your anchor
var $yourAnchor = $('.test')
// copy data attribute
var $hdata = $yourAnchor.data('value');
// remove attribute
$yourAnchor.removeAttr('data-value');
// insert html
$yourAnchor.html('<img src="' + $hdata + '">');
// If It has multiple hyperlink tag then
$('a').each(function(){
if($(this).data('value')!=undefined){
$(this).append(function(){
return '<img src="'+$(this).attr("data-value")+'">';
}).removeAttr("data-value");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="test" data-value="/content/dam/tetris-templating/framework/svg/ic-editorial-07.svg"></a>
I have a lot of streams.
<a id="stream1" href="http://server/stream1/parameters">name_1</a>
<a id="stream2" href="http://server/stream2/parameters">name_2</a>
...
<a id="stream99" href="http://server/stream99/parameters">name_99</a>
I want to hide "http://server/stream99/parameters"
How to do a function to paste "id" into href
eg:
<a id="stream1" href="#">name_1</a>
using something like
function somefunction(a) {
document.getElementById('a').innerHTML = '<a href="http://server/'+id+'/parameters">';
}
Javascript or jquery will make the link and paste the "id".
Or perhaps I can somehow use a different method to hide links?
Like this - do note that this is easy to decipher by inspecting the code.
Alternatively use a server redirect and just send some encrypted ID
$(function() {
$(".stream").on("click", function(e) {
e.preventDefault();
location = "http://server/" + this.id + "/parameters";
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<a id="stream1" class="stream" href="#">name_1</a><br>
<a id="stream2" class="stream" href="#">name_2</a><br>
<a id="stream3" class="stream" href="#">name_3</a><br>
<a id="stream4" class="stream" href="#">name_4</a><br>
I'm having problems with a JS link.
I have the following code:
window.addEvent('domready', function() {
$('province_aw').addEvent('change', function() {
var index = this.options[this.selectedIndex].value;
var b = document.getElementById("choice_2").value;
var link = "index.php?option=com_chronoforms&chronoform=listSpecific-3&id_province="+index+"&id_ch="+b;
document.getElementById('link1').href = "index.php?option=com_chronoforms&chronoform=listSpecific-3&id_province="+index+"&id_ch="+b;
});
});
with this link:
<a class="jcepopup" id="link1" href="" rel="{handler:'iframe'}"> <input type='button' name='prueba' id='prueba' value='...' /> </a>
The code is actually changing the link of the < a > tag, however, when I click on the button, nothing happens, it keeps sending me to the original href (which is the homepage).
You should use href="javascript:void(0);"