最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to access JavaScript variable in HTML tags? - Stack Overflow

programmeradmin3浏览0评论

I am trying to access JavaScript variable in the href attribute of anchor tag.

JavaScript Function:

<script type="text/javascript"> 
function fun(ReqTextbox) 

{ 

var queueData =document.getElementById(ResTextbox).value; 
//some more code 

} 
</script>

HTML code:

<body> 
    <input type="text" value="<%=dynamicValue%>" id="<%=dynamicId%>"/> 
    <a href="servlet?variablename="<%Eval(queueData)%>" onclick=fun('<%=dynamicvalue%>');>LINK</a>     
</body> 

I know that I am going to wrong at variablename="<%Eval(queueData)%>".

Can someone please help me how to access and pass JavaScript variable as a query string parameter?

I am trying to access JavaScript variable in the href attribute of anchor tag.

JavaScript Function:

<script type="text/javascript"> 
function fun(ReqTextbox) 

{ 

var queueData =document.getElementById(ResTextbox).value; 
//some more code 

} 
</script>

HTML code:

<body> 
    <input type="text" value="<%=dynamicValue%>" id="<%=dynamicId%>"/> 
    <a href="servlet?variablename="<%Eval(queueData)%>" onclick=fun('<%=dynamicvalue%>');>LINK</a>     
</body> 

I know that I am going to wrong at variablename="<%Eval(queueData)%>".

Can someone please help me how to access and pass JavaScript variable as a query string parameter?

Share Improve this question edited Mar 26, 2014 at 23:20 Michael 4,0004 gold badges32 silver badges46 bronze badges asked Mar 12, 2014 at 10:10 Sudhakar BynaSudhakar Byna 1291 gold badge3 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

First, I think you made a typo :

function fun(ReqTextbox) { 
    var queueData = document.getElementById(ResTextbox).value; 
    //some more code
} 

You get ReqTextbox parameter but you use ResTextbox. Then, since Javascript is client-sided, you have to manually update the href tag using href attribute. So your function would be like :

function fun(ReqTextbox) { 
    var queueData = document.getElementById(ReqTextbox).value; 
    document.getElementById('myAnchor').href = "servlet?variablename=" + queueData;
    //some more code
} 

And give your anchor tag an id, myAnchor in my example.

Modify HTML code as given below - 

<body> 
<input type="text" value="<%=dynamicValue%>" id="<%=dynamicId%>"/>
<div id="example2"> 
<a href="servlet?variablename="<%Eval(queueData)%>" onclick=fun('<%=dynamicvalue%>');>LINK</a> </div>
</body> 

Use this to pass query string - 

$("#example2 a").attr("href", url + "?variablename=" + queueData);

If I understand you correctly inside fun() function try to use:

var queueData = this.getAttribute('id');

That way you will get id value when runnning onclick() function.

发布评论

评论列表(0)

  1. 暂无评论