0

I want to change THISVALUE using a textbox and submit button, which then refreshes the data on the page:

<form id="newssearch" action="#">
  <input type="text" size="30" maxlength="155" name="search" id="search" />
  <input type="button" name="submit" value="Search" onclick="showElements();" />
</form>

<div class="sm" data-type="static" data-symbol="THISVALUE" data-size="medium" data-logscale="on" data-chart-type="ca" data-timeframe="1y"></div>

<div class="sm" data-type="news" data-symbol="THISVALUE"></div>

Also: you press the button and it refreshes the page with the new data-symbol value. That value stays for the next visit to the page or until another search is performed.

Perhaps it would be better to do this in php?

6
  • Can you attach the form? Commented Jun 10, 2016 at 18:22
  • Make sure you provide some code for the community to be able to help you out. Commented Jun 10, 2016 at 18:23
  • Can you paste the code instead of attaching a pic. Commented Jun 10, 2016 at 18:25
  • 3
    ^^^ To all the comments above: when you see an new user write "Here's the form:" and you see no form, try editing the question, chances are OP did not know how to use code blocks and therefor no code was visible... Commented Jun 10, 2016 at 18:32
  • Will definitely keep that in mind for next time. Commented Jun 10, 2016 at 18:33

2 Answers 2

1

You can also use the setAttribute() function

function showElements(){

  document.getElementsByClassName('blah')[0].setAttribute("data-symbol",document.getElementById('search').value);

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

Comments

1

First off - data-symbol is not an element. It is an attribute and to be more specific - a data attribute.

Learn more about data attributes here: Using data attributes | MDN


I assume you want the data submitted in the form to get into the data-symbol attribute.

Checkout the working code snippet below:

function showElements(){

  // just copy over the search text into the data attribute
  document.getElementsByClassName('blah')[0].dataset.symbol = document.getElementById('search').value;

}
<div class="blah" data-type="cur" data-symbol="THISVALUE"></div>


<form id="newssearch" action="#">

  <input type="text" size="30" maxlength="155" name="search" id="search" />
  <input type="button" name="submit" value="Search" onclick="showElements();" /> <!-- no need to pass any arguments to this function, we can get data using element ID -->

</form>


Check the result using Developer Tools (I have used Chrome here):

enter image description here

12 Comments

sorry a bit irrelevant to the question being asked here. How do you add run code snippet to your answer? I checked the help page but couldn't find anything
this is off topic, but Is that a stackoverflow version of jsfiddle?
Google is your friend :) You can always Google it if you don't find it in help section. Here you go: blog.stackoverflow.com/2014/09/…
@AdamBuchananSmith ^
@user6439245 FYI ^
|

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.